在JQUERY中如何找到页面中的所有ajax调用都已完成?

时间:2021-12-04 21:00:58

In JQUERY how to find that all ajax calls in a page have been completed?

在JQUERY中如何找到页面中的所有ajax调用都已完成?

I want to execute a method after all the ajax call are completed.

我希望在完成所有ajax调用后执行一个方法。

5 个解决方案

#1


4  

If you are running a number of requests and you want to know when they're all done, you're looking for $.ajaxStop(), which is a global event and which is "called when all AJAX requests have completed", according to the documentation.

如果你正在运行许多请求而你想知道他们什么时候完成,那么你正在寻找$ .ajaxStop(),它是一个全局事件,并且“在所有AJAX请求完成后调用”,根据到文档。

#2


3  

If you know how many ajax call you have sent by increment some variable on each call. You can use another variable in success of ajax call to count how many times success is called. When both variables have same value you can assume all ajax calls are completed.

如果您通过在每次调用中递增一些变量来了解已发送的ajax调用次数。您可以在ajax调用成功时使用另一个变量来计算成功调用的次数。当两个变量具有相同的值时,您可以假设所有的ajax调用都已完成。

#3


2  

check on this article http://lostechies.com/joshuaflanagan/2011/10/20/coordinating-multiple-ajax-requests-with-jquery-when/

查看这篇文章http://lostechies.com/joshuaflanagan/2011/10/20/coordinating-multiple-ajax-requests-with-jquery-when/

This is the code from their example:

这是他们示例中的代码:

$.when( getTweets('austintexasgov'),
        getTweets('greenling_com'),
        getTweets('themomandpops')
      ).done(function(atxArgs, greenlingArgs, momandpopsArgs){
    var allTweets = [].concat(atxArgs[0]).concat(greenlingArgs[0]).concat(momandpopsArgs[0]);
    var sortedTweets = sortTweets(allTweets);
    showTweets(sortedTweets);
      });

var getTweets = function(user){
    var url='http://twitter.com/status/user_timeline/' + user + '.json';
    return $.get(url, {count:5}, null, 'jsonp');
}

Worked perfect for me

工作对我来说很完美

#4


1  

To monitor tasks like this, you can always build a state machine. On each ajax call, add a beforeSend() callback and a complete() callback to update state to some state machine. The completed method on your state machine will conditionally execute your code if the count is at 0.

要监视这样的任务,您始终可以构建状态机。在每次ajax调用时,添加beforeSend()回调和complete()回调以将状态更新到某个状态机。如果计数为0,则状态机上的已完成方法将有条件地执行您的代码。

Note.... this will ONLY work if there are ajax calls with overlapping execution (running in parallel). If you have ajax calls running in serial, your state machine will execute each time all active ajax calls are in a completed state.

注意....如果存在重叠执行的ajax调用(并行运行),这将仅起作用。如果您有串行运行的ajax调用,则每次所有活动的ajax调用都处于已完成状态时,您的状态机将执行。

To work correctly with serial ajax calls, you would have to know in advance how many ajax calls you're going to make, and test against that number.

要正确使用串行ajax调用,您必须事先知道要进行多少个ajax调用,并针对该数字进行测试。

$.ajax({
    url: "test.html",
}).beforeSend(function() {
    ajaxState.started();
}).complete(function() {
    ajaxState.completed();
});

var ajaxState = {
    active: 0,
    started: function() {
        ajaxState.active++;
    },
    completed: function() {
        ajaxState.active--;
        if (!ajaxState.active) ajaxState.onComplete();
    },
    onComplete: function() {
        // Your code here.
    }
};

#5


0  

Maybe you call a method in the last Ajax method? There is no way to know if you've got any Ajax call made by a button for example that will be generated dynamically or whatever.

也许你在最后一个Ajax方法中调用一个方法?没有办法知道你是否通过按钮进行任何Ajax调用,例如动态生成或者其他任何东西。

If you are sure about your last Ajax call, call a method on success.

如果您确定上次调用Ajax,请在成功时调用方法。

$.ajax({
    url: "test.html",
    type: "POST",
    data: {id : menuId},
}).done(function() {
    callMethod();
});

#1


4  

If you are running a number of requests and you want to know when they're all done, you're looking for $.ajaxStop(), which is a global event and which is "called when all AJAX requests have completed", according to the documentation.

如果你正在运行许多请求而你想知道他们什么时候完成,那么你正在寻找$ .ajaxStop(),它是一个全局事件,并且“在所有AJAX请求完成后调用”,根据到文档。

#2


3  

If you know how many ajax call you have sent by increment some variable on each call. You can use another variable in success of ajax call to count how many times success is called. When both variables have same value you can assume all ajax calls are completed.

如果您通过在每次调用中递增一些变量来了解已发送的ajax调用次数。您可以在ajax调用成功时使用另一个变量来计算成功调用的次数。当两个变量具有相同的值时,您可以假设所有的ajax调用都已完成。

#3


2  

check on this article http://lostechies.com/joshuaflanagan/2011/10/20/coordinating-multiple-ajax-requests-with-jquery-when/

查看这篇文章http://lostechies.com/joshuaflanagan/2011/10/20/coordinating-multiple-ajax-requests-with-jquery-when/

This is the code from their example:

这是他们示例中的代码:

$.when( getTweets('austintexasgov'),
        getTweets('greenling_com'),
        getTweets('themomandpops')
      ).done(function(atxArgs, greenlingArgs, momandpopsArgs){
    var allTweets = [].concat(atxArgs[0]).concat(greenlingArgs[0]).concat(momandpopsArgs[0]);
    var sortedTweets = sortTweets(allTweets);
    showTweets(sortedTweets);
      });

var getTweets = function(user){
    var url='http://twitter.com/status/user_timeline/' + user + '.json';
    return $.get(url, {count:5}, null, 'jsonp');
}

Worked perfect for me

工作对我来说很完美

#4


1  

To monitor tasks like this, you can always build a state machine. On each ajax call, add a beforeSend() callback and a complete() callback to update state to some state machine. The completed method on your state machine will conditionally execute your code if the count is at 0.

要监视这样的任务,您始终可以构建状态机。在每次ajax调用时,添加beforeSend()回调和complete()回调以将状态更新到某个状态机。如果计数为0,则状态机上的已完成方法将有条件地执行您的代码。

Note.... this will ONLY work if there are ajax calls with overlapping execution (running in parallel). If you have ajax calls running in serial, your state machine will execute each time all active ajax calls are in a completed state.

注意....如果存在重叠执行的ajax调用(并行运行),这将仅起作用。如果您有串行运行的ajax调用,则每次所有活动的ajax调用都处于已完成状态时,您的状态机将执行。

To work correctly with serial ajax calls, you would have to know in advance how many ajax calls you're going to make, and test against that number.

要正确使用串行ajax调用,您必须事先知道要进行多少个ajax调用,并针对该数字进行测试。

$.ajax({
    url: "test.html",
}).beforeSend(function() {
    ajaxState.started();
}).complete(function() {
    ajaxState.completed();
});

var ajaxState = {
    active: 0,
    started: function() {
        ajaxState.active++;
    },
    completed: function() {
        ajaxState.active--;
        if (!ajaxState.active) ajaxState.onComplete();
    },
    onComplete: function() {
        // Your code here.
    }
};

#5


0  

Maybe you call a method in the last Ajax method? There is no way to know if you've got any Ajax call made by a button for example that will be generated dynamically or whatever.

也许你在最后一个Ajax方法中调用一个方法?没有办法知道你是否通过按钮进行任何Ajax调用,例如动态生成或者其他任何东西。

If you are sure about your last Ajax call, call a method on success.

如果您确定上次调用Ajax,请在成功时调用方法。

$.ajax({
    url: "test.html",
    type: "POST",
    data: {id : menuId},
}).done(function() {
    callMethod();
});