jQuery的:等到所有Ajax调用完成再继续 [英] jquery : wait until all ajax calls finish then continue

查看:126
本文介绍了jQuery的:等到所有Ajax调用完成再继续的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我的document.ready一些Ajax调用()

I have some ajax calls in my document.ready()

这样的:

for (j=1; j <= 7; j++){
  (function(index) {
    $.getJSON('my.php', {id:index}, 
      function(data) {
         $.each(data, function(index2, array){
         ........
         });  
      });
    })(j)
} 

//DO NOT CONTINUE UNTIL FINISH AJAX CALLS   

 ........
MORE JQUERY CODE

我怎么能强制等待,而不是继续下去,直到我们得到了所有的回调从Ajax请求?

How can i force it to wait and not continue until we get all the call backs from the ajax requests ?

推荐答案

我不喜欢任何答案可言,(因为jQuery的1.5或更高版本)的最好方法是使用延迟的对象,那些对象来处理异步调用,您能解决:

I do not like any answer at all, the best way (since Jquery 1.5+) is to use Deferred objects, those are objects to manipulate async calls, you can solve :

$.when($.ajax("/page1.php"), $.ajax("/page2.php"))
  .then(myFunc, myFailure);

2 Ajax调用后,此方法myFunc的执行,并myFailure如果任何人有一个错误。

This way myFunc executes after the 2 ajax calls are made, and myFailure if either one has an error.

您可以阅读更多关于它在jQuery的官方文档: JQuery的延迟的对象

You can read more about it in the jquery official documentation:JQuery Deferred Object

我写这篇文章的解决方案,如果有人看到这个帖子可能会有所帮助。

I write this solution if anyone sees this post it may be helpful.

对不起我的英语水平:P

Sorry my english :P

这篇关于jQuery的:等到所有Ajax调用完成再继续的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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