简体   繁体   English

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

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

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调用后执行一个方法。

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

If you know how many ajax call you have sent by increment some variable on each call . 如果您知道通过在每次调用时增加一些变量来发送多少ajax 调用 You can use another variable in success of ajax call to count how many times success is called . 您可以在ajax调用成功时使用另一个变量来计算成功调用的次数 When both variables have same value you can assume all ajax calls are completed. 当两个变量具有相同的值时,您可以假设所有的ajax调用都已完成。

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 工作对我来说很完美

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. 在每次ajax调用时,添加beforeSend()回调和complete()回调以将状态更新到某个状态机。 The completed method on your state machine will conditionally execute your code if the count is at 0. 如果计数为0,则状态机上的completed方法将有条件地执行您的代码。

Note.... this will ONLY work if there are ajax calls with overlapping execution (running in parallel). 注意......如果存在重叠执行的ajax调用(并行运行),这将仅起作用。 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调用都处于已完成状态时,您的状态机将执行。

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

Maybe you call a method in the last Ajax method? 也许你在最后一个Ajax方法中调用一个方法? 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调用,例如动态生成或其他任何操作。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 C#Web请求-如何等待页面及其所有后续jquery / ajax调用完成并且页面已完全加载 - C# web request - how to wait until page and all of its subsequent jquery/ajax calls have completed and page has fully loaded 确保所有异步调用均已在jQuery中完成 - Make sure all async calls have been completed in jQuery 如果没有jQuery,javascript如何检查多个ajax调用是否已经完成,一旦有了,就执行一段代码? - Without jQuery, how can javascript check that multiple ajax calls have been completed, and once they have, execute a piece of code? 如何知道JQuery每个循环中的所有ajax调用何时完成? - How to know when all the ajax calls in JQuery each loop are completed? 完成所有ajax调用后如何运行函数 - How to run a function once all the ajax calls have been done 在所有先前的ajax调用完成后触发ajax调用 - Fire ajax call after all the previous ajax calls have completed 当我知道所有异步调用都已完成时,如何运行回调? - How can I run a callback when I know all asynchronous calls have been completed? jQuery:如何检查以确保所有异步操作均已完成 - jQuery: how to check to make sure all asynchronous actions have been completed 如何查找是否所有动画都已完成 - How to find if all animations have completed 如何确保所有ajax调用均已成功执行(未完成) - How to make sure all ajax calls got executed successfully ( not completed)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM