使用,如其文档页中所指出的。它只设置默认设置,如果某些请求覆盖它们,则会造成混乱。
我已经迟到了,但只是为了将来的参考,如果有人在寻找解决同样问题的方法,这就是我要做的事情,这是我的灵感所在,与之前的答案大致相同,但更完整。// Automatically cancel unfinished ajax requests // when the user navigates elsewhere.(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});
});
var abort = function() {
$.each(xhrPool, function(idx, jqXHR) {
jqXHR.abort();
});
};
var oldbeforeunload = window.onbeforeunload;
window.onbeforeunload = function() {
var r = oldbeforeunload ? oldbeforeunload() : undefined;
if (r == undefined) {
// only cancel requests if there is no prompt to stay on the page
// if there is a prompt, it will likely give the requests enough time to finish
abort();
}
return r;
}})(jQuery);