Javascript 全局中止所有 jQuery AJAX 请求
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3456427/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Abort all jQuery AJAX requests globally
提问by Robert W
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(). If the user clicks a certain button we need to halt all ongoing requests.
我问的原因是我们有一个相当复杂的应用程序,我们使用 setTimeOut() 在后台运行许多不同的 Ajax 请求。如果用户单击某个按钮,我们需要停止所有正在进行的请求。
采纳答案by Sarfraz
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 abortmethod on that to abort the request any time.
通过这种方式,您需要为 ajax 请求创建一个变量,然后您可以随时使用该abort方法中止请求。
Also have a look at:
也看看:
回答by 7wp
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()每个处理程序来做到这一点。
回答by DJ.
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/.
回答by Mnebuerquo
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.onbeforeunloadas 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();to abort them all. Make sure you add a .fail(jqXHRFailCallback)to your ajax requests. The callback will get 'abort' as textStatusso you know what happened:
然后你可以打电话window.abortAllMyAjaxRequests();中止它们。确保将 a 添加.fail(jqXHRFailCallback)到您的 ajax 请求中。回调将“中止”,textStatus因为您知道发生了什么:
function jqXHRFailCallback(jqXHR, textStatus){
// textStatus === 'abort'
}

