简体   繁体   English

全局中止所有jQuery AJAX请求

[英]Abort all jQuery AJAX requests globally

Is there a way to abort all Ajax requests globally without a handle on the request object? 有没有办法在没有请求对象句柄的情况下全局中止所有Ajax请求?

The reason I ask is that we have quite a complex application where we are running a number of different Ajax requests in the background by using setTimeOut(). 我问的原因是我们有一个非常复杂的应用程序,我们使用setTimeOut()在后台运行许多不同的Ajax请求。 If the user clicks a certain button we need to halt all ongoing requests. 如果用户单击某个按钮,我们需要暂停所有正在进行的请求。

You need to call abort() method: 你需要调用abort()方法:

var request = $.ajax({
    type: 'POST',
    url: 'someurl',
    success: function(result){..........}
});

After that you can abort the request: 之后,您可以中止请求:

request.abort();

This way you need to create a variable for your ajax request and then you can use the abort method on that to abort the request any time. 这样,您需要为ajax请求创建一个变量,然后您可以使用abort方法随时中止请求。

Also have a look at: 另外看看:

You cannot abort all active Ajax requests if you are not tracking the handles to them. 如果您没有跟踪它们的句柄,则无法中止所有活动的Ajax请求。

But if you are tracking it, then yes you can do it, by looping through your handlers and calling .abort() on each one. 但是如果你正在跟踪它,那么你可以通过循环处理程序并在每个处理程序上调用.abort()来实现它。

You can use this script: 您可以使用此脚本:

// $.xhrPool and $.ajaxSetup are the solution
$.xhrPool = [];
$.xhrPool.abortAll = function() {
    $(this).each(function(idx, jqXHR) {
        jqXHR.abort();
    });
    $.xhrPool = [];
};

$.ajaxSetup({
    beforeSend: function(jqXHR) {
        $.xhrPool.push(jqXHR);
    },
    complete: function(jqXHR) {
        var index = $.xhrPool.indexOf(jqXHR);
        if (index > -1) {
            $.xhrPool.splice(index, 1);
        }
    }
});

Check the result at http://jsfiddle.net/s4pbn/3/ . 检查http://jsfiddle.net/s4pbn/3/上的结果。

This answer to a related question is what worked for me: 对相关问题的回答是对我有用的:

https://stackoverflow.com/a/10701856/5114 https://stackoverflow.com/a/10701856/5114

Note the first line where the @grr says: "Using ajaxSetup is not correct" 注意@grr说的第一行:“使用ajaxSetup不正确”

You can adapt his answer to add your own function to window if you want to call it yourself rather than use window.onbeforeunload as they do. 如果你想自己调用它而不是像他们那样使用window.onbeforeunload ,你可以调整他的答案来将你自己的函数添加到窗口。

// Most of this is copied from @grr verbatim:
(function($) {
  var xhrPool = [];
  $(document).ajaxSend(function(e, jqXHR, options){
    xhrPool.push(jqXHR);
  });
  $(document).ajaxComplete(function(e, jqXHR, options) {
    xhrPool = $.grep(xhrPool, function(x){return x!=jqXHR});
  });
  // I changed the name of the abort function here:
  window.abortAllMyAjaxRequests = function() {
    $.each(xhrPool, function(idx, jqXHR) {
      jqXHR.abort();
    });
  };
})(jQuery);

Then you can call window.abortAllMyAjaxRequests(); 然后你可以调用window.abortAllMyAjaxRequests(); to abort them all. 中止他们。 Make sure you add a .fail(jqXHRFailCallback) to your ajax requests. 确保在ajax请求中添加.fail(jqXHRFailCallback) The callback will get 'abort' as textStatus so you know what happened: 回调将作为textStatus '中止',所以你知道发生了什么:

function jqXHRFailCallback(jqXHR, textStatus){
  // textStatus === 'abort'
}

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM