递归阿贾克斯一旦成功 [英] Recursive Ajax Upon Success

查看:108
本文介绍了递归阿贾克斯一旦成功的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图让递归Ajax请求在从每个请求返回,我会倾向于堆栈溢出每个成功响应?如果是这样,你必须这样做没有更好的办法?我的要求是先执行一个Ajax请求,如果返回的JSON是没有对相同的参数...等执行另一个Ajax请求,直到我得到一个完成标志。

 去:函数(R){
    Ext.Ajax.request({
        网址:'的Bleh,
        成功:函数(响应){
            VAR数据= Ext.de code(response.responseText); //德code JSON成目标
            r.update();
            如果(data.isDone!)去(R);
        }
    });
}
 

解决方案

这是不实际递归所以堆栈溢出的危险。它可能看起来像递归,但因为Ajax调用是asycnchronous,你的去()函数只启动Ajax调用,然后去()函数结束的时候了,而AJAX调用正在进行中。因此,成功的处理程序被调用后不久去()的功能已经完成。因此,它实际上没有递归,不存在堆栈堆积。

这可能看起来像从的code视觉效果递归,但因为Ajax调用是异步的,成功的处理程序后,去()函数返回,没有堆堆积。

I'm trying to make an ajax request recursively upon each success response that is returned from each request Would I be prone to stack overflows? If so do you have any better way of doing this? My requirements is to initially perform an ajax request and if the json returned is not done perform another ajax request with the same parameters... etc until i get a done flag.

go: function (r) {
    Ext.Ajax.request({
        url: 'bleh',
        success: function (response) {
            var data = Ext.decode(response.responseText); // decode json into object
            r.update();
            if (!data.isDone) go(r);
        }
    });
}

解决方案

This is not actually recursion so there is no danger of stack overflow. It may look like recursion, but because the ajax call is asycnchronous, your go() function only starts the ajax call and then the go() function finishes right away while the ajax call is underway. Thus, the success handler is called long after the go() function has already finished. so, it's not actually recursion and there is no stack buildup.

It may look like recursion from the visual effects of the code, but because the ajax call is asynchronous and the success handler is called long after the go() function has returned, there is no stack buildup.

这篇关于递归阿贾克斯一旦成功的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆