admin管理员组文章数量:1415684
I have been working with AJAX for a while now, but in limited and simple ways. I use JQuery
Currently I am debugging a web application. The client side code uses JavaScript and JQuery. I noticed that in this application it is possible to have multiple AJAX requests sent out at the same time (one right after the other). My concern is that because AJAX is asynchronous that the AJAX request might not be pleted in the correct order. I was wondering if the proper AJAX callbacks will be executed regardless of witch response is returns first or the call back functions are executed in FIFO manner
Let me elaborate
I have 2 AJAX requests A and B. Both A and B have there own call back functions. The app first makes request A then immediately afterward makes request B. Now the App expects A to return first. Now my question is what if B returns first. Which Call back will be executed?
I did some research and could not find any info on this issue. So I assumed that the browser would coordinate the callbacks. To make sure I wrote a little test. My test showed that regardless of which response returns first the first requests call back is always used first.
My question is what is the behavior? Also what techniques or methods are used to avoid such a case.
I have been working with AJAX for a while now, but in limited and simple ways. I use JQuery
Currently I am debugging a web application. The client side code uses JavaScript and JQuery. I noticed that in this application it is possible to have multiple AJAX requests sent out at the same time (one right after the other). My concern is that because AJAX is asynchronous that the AJAX request might not be pleted in the correct order. I was wondering if the proper AJAX callbacks will be executed regardless of witch response is returns first or the call back functions are executed in FIFO manner
Let me elaborate
I have 2 AJAX requests A and B. Both A and B have there own call back functions. The app first makes request A then immediately afterward makes request B. Now the App expects A to return first. Now my question is what if B returns first. Which Call back will be executed?
I did some research and could not find any info on this issue. So I assumed that the browser would coordinate the callbacks. To make sure I wrote a little test. My test showed that regardless of which response returns first the first requests call back is always used first.
My question is what is the behavior? Also what techniques or methods are used to avoid such a case.
Share Improve this question edited Jan 5, 2014 at 19:05 Majid 14.3k16 gold badges81 silver badges116 bronze badges asked Sep 28, 2011 at 14:35 weAreInItTogetherweAreInItTogether 992 silver badges8 bronze badges2 Answers
Reset to default 4Look at the jQuery promise/deferred objects, they allow you to control this exact behavior.
$.when( $.ajax("test.aspx") ).then( $.ajax("test2.aspx") );
http://api.jquery./category/deferred-object/
As you described the flow - if the request B returns first, then its callback will be called first.
You can always call the second ajax request when the first one suceed, for example:
function callbackA() { return true; }
function callbackB() { return true; }
$.ajax({url: '/my/url', data: {mydata: mydata}, success: function(data) {
callbackA(data);
$.ajax({url: '/my/url2', data: {mydata2: mydata2}, success: function(data) {}
callbackB(data);
});
});
本文标签: javascriptAJAX asynchronous response callbacksStack Overflow
版权声明:本文标题:javascript - AJAX asynchronous response callbacks - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745239354a2649231.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论