jquery:等到所有ajax调用完成后再继续[duplicate]

时间:2022-08-23 18:00:30

This question already has an answer here:

这个问题已经有了答案:

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

文档中有一些ajax调用。ready()

like :

如:

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

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

我如何能迫使它等待而不继续,直到我们从ajax请求中获得所有回调?

5 个解决方案

#1


16  

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 :

我不喜欢任何答案,最好的方法(因为Jquery 1.5+)是使用递延对象,这些对象是用来操作异步调用的对象,您可以解决:

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

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

这样,myFunc在执行两个ajax调用后执行,如果其中一个调用有错误,则myFailure执行。

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

您可以在jquery官方文档:jquery Deferred对象中了解更多

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

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

Sorry my english :P

对不起我的英语:P

#2


6  

First off, you might want to consider doing the startup code in a single call.
Second: Instead of waiting just call another function call. for the above code it should look something like:

首先,您可能需要考虑在一次调用中执行启动代码。第二:不要等待,只需调用另一个函数调用。对于上述代码,它应该如下所示:

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

         if (j === 7) {
            initDoneDoMoreStuff()
         }
      });
    })(j)
} 

or trigger:

或触发:

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

         if (j === 7) {
            $(document).trigger("initdone");
         }
      });
    })(j)
}

$(document).bind("initdone", function() {....});

#3


3  

It wasn´t easy for me to do it, but maybe you find my code useful. It´s a process to submit all forms in the dom and after all of them are stored, redirect the user to another page.

不是´t容易做到,但也许你觉得我的代码很有用。´s流程提交所有形式的dom和所有的存储后,将用户重定向到另一个页面。

formulario = $('form');
formulariolongitud = formulario.length;
i = 0;

formulario.each(function(index, value) {
    $(this).ajaxSubmit({
        async: false,//not sure i
        success: function() {
            i++; 

            if(i === formulario.length) {//this is the last one to submit
                window.location.replace(whatever);
            }
        } 
    });                         
});

#4


1  

you can use sucess (ie a callback function of ajax jquery) like below :

您可以使用sucess(即ajax jquery的回调函数),如下所示:

$.ajax({
  url: url,
 dataType: 'json',
 data: data,
 success: function(data){
 //Write your code here.
    }
});

You can get documentation of ajax below -

你可以在下面得到ajax的文档

ajax

ajax

#5


0  

The problem with something like if (j == 7) is the situation when the 7th request is very very fast, and even one of the others is slow. Even though you have queued it up last, it might not be the last to complete.

类似if (j == 7)的问题是第7个请求非常非常快的情况,甚至其中一个请求也很慢。即使您将它排在最后,它也可能不是最后一个完成的。

This answer seems to work for all conditions: https://*.com/a/3709809/813154

这个答案似乎适用于所有条件:https://*.com/a/3709809/813154。

#1


16  

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 :

我不喜欢任何答案,最好的方法(因为Jquery 1.5+)是使用递延对象,这些对象是用来操作异步调用的对象,您可以解决:

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

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

这样,myFunc在执行两个ajax调用后执行,如果其中一个调用有错误,则myFailure执行。

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

您可以在jquery官方文档:jquery Deferred对象中了解更多

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

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

Sorry my english :P

对不起我的英语:P

#2


6  

First off, you might want to consider doing the startup code in a single call.
Second: Instead of waiting just call another function call. for the above code it should look something like:

首先,您可能需要考虑在一次调用中执行启动代码。第二:不要等待,只需调用另一个函数调用。对于上述代码,它应该如下所示:

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

         if (j === 7) {
            initDoneDoMoreStuff()
         }
      });
    })(j)
} 

or trigger:

或触发:

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

         if (j === 7) {
            $(document).trigger("initdone");
         }
      });
    })(j)
}

$(document).bind("initdone", function() {....});

#3


3  

It wasn´t easy for me to do it, but maybe you find my code useful. It´s a process to submit all forms in the dom and after all of them are stored, redirect the user to another page.

不是´t容易做到,但也许你觉得我的代码很有用。´s流程提交所有形式的dom和所有的存储后,将用户重定向到另一个页面。

formulario = $('form');
formulariolongitud = formulario.length;
i = 0;

formulario.each(function(index, value) {
    $(this).ajaxSubmit({
        async: false,//not sure i
        success: function() {
            i++; 

            if(i === formulario.length) {//this is the last one to submit
                window.location.replace(whatever);
            }
        } 
    });                         
});

#4


1  

you can use sucess (ie a callback function of ajax jquery) like below :

您可以使用sucess(即ajax jquery的回调函数),如下所示:

$.ajax({
  url: url,
 dataType: 'json',
 data: data,
 success: function(data){
 //Write your code here.
    }
});

You can get documentation of ajax below -

你可以在下面得到ajax的文档

ajax

ajax

#5


0  

The problem with something like if (j == 7) is the situation when the 7th request is very very fast, and even one of the others is slow. Even though you have queued it up last, it might not be the last to complete.

类似if (j == 7)的问题是第7个请求非常非常快的情况,甚至其中一个请求也很慢。即使您将它排在最后,它也可能不是最后一个完成的。

This answer seems to work for all conditions: https://*.com/a/3709809/813154

这个答案似乎适用于所有条件:https://*.com/a/3709809/813154。