Recursive Ajax Upon Success

可紊 提交于 2019-12-24 02:33:18

问题


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);
        }
    });
}

回答1:


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.



来源:https://stackoverflow.com/questions/10019227/recursive-ajax-upon-success

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!