Is there any easy way to abandon all outstanding jQuery AJAX requests? My application is capable of pulling off a lot of simultaneous requests, so race becomes problematic. I've put in hackish solutions (namely, putting in a flag that is checked upon request completion), but it would be much nicer to get a global stop all outstanding requests function.
有没有简单的方法可以放弃所有优秀的jQuery AJAX请求?我的应用程序能够提取大量的同时请求,因此竞争变得有问题。我已经提出了一些hackish解决方案(即,在请求完成时放入一个标记),但是全局停止所有未完成的请求功能会更好。
6 个解决方案
#1
12
Assign every ajax request as an element of an array:
将每个ajax请求分配为数组的元素:
var requests = [];
requests.push($.ajax({
type: 'POST',
url: '...',
success: successfunc
});
);
And to kill:
要杀了:
$.each(requests, function(i, v) {
v.abort();
});
#2
3
as the others have said, use the .abort() method. However, this ONLY works on calls within the same domain. once you enter into cross-site calls (with jsonp), then the .abort() method is delegated to null, so wont actually fire/work.
正如其他人所说,使用.abort()方法。但是,这仅适用于同一域内的呼叫。一旦你进入跨站点调用(使用jsonp),那么.abort()方法被委托为null,因此实际上不会触发/工作。
worth noting as this caused me endless debugging time many a small moon ago :)
值得注意的是,这让我在很多小月前的无尽调试时间:)
jim
吉姆
#3
3
Based on Ken Redler's solution, I put this in my initialization:
根据肯·雷德勒的解决方案,我把它放在我的初始化中:
window.requests = []
$.ajaxSetup({
beforeSend: function(xhr){
var new_request_list = [xhr]
$.each(window.requests, function(k,v){
if(v.readyState != 4) new_request_list.push(v)
})
window.requests = new_request_list
}
})
I put in the snippet with new_request_list
just to keep the global variable from getting crowded with completed XHR objects.
我在new_request_list中添加了片段,以防止全局变量被完整的XHR对象挤占。
#4
2
XMLHttpRequest has an abort() function. $.ajax lets you get your hands on the xhr object. keep a record of all outstanding xhr's and go xhr.abort()
for any you want to terminate.
XMLHttpRequest有一个abort()函数。 $ .ajax让您可以轻松使用xhr对象。保留所有未完成的xhr的记录,并为任何想要终止的人输入xhr.abort()。
#5
1
I would say store all ajax requests in an array. When you need to kill then just loop through all values in array and call abort(). When a request is completed or aborted just pop from the array.
我想说将所有ajax请求存储在一个数组中。当你需要kill时,只需遍历数组中的所有值并调用abort()。当请求完成或中止时,只需从数组中弹出。
#6
1
I found the following on jsfiddle. It is mostly the same as above, except it will remove each request once they are complete. The solutions above just keep adding to the array. So over time, it could get huge if you are making a lot of ajax calls. You call $.xhrPool.abortAll();
when you want to abort all outstanding requests.
我在jsfiddle上找到了以下内容。它与上面大致相同,只要它们在完成后删除每个请求。上面的解决方案只是不断添加到阵列。所以随着时间的推移,如果你正在进行大量的ajax调用,它可能会变得很大。你打电话给$ .xhrPool.abortAll();当你想要中止所有未完成的请求时。
// $.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);
}
}
});
#1
12
Assign every ajax request as an element of an array:
将每个ajax请求分配为数组的元素:
var requests = [];
requests.push($.ajax({
type: 'POST',
url: '...',
success: successfunc
});
);
And to kill:
要杀了:
$.each(requests, function(i, v) {
v.abort();
});
#2
3
as the others have said, use the .abort() method. However, this ONLY works on calls within the same domain. once you enter into cross-site calls (with jsonp), then the .abort() method is delegated to null, so wont actually fire/work.
正如其他人所说,使用.abort()方法。但是,这仅适用于同一域内的呼叫。一旦你进入跨站点调用(使用jsonp),那么.abort()方法被委托为null,因此实际上不会触发/工作。
worth noting as this caused me endless debugging time many a small moon ago :)
值得注意的是,这让我在很多小月前的无尽调试时间:)
jim
吉姆
#3
3
Based on Ken Redler's solution, I put this in my initialization:
根据肯·雷德勒的解决方案,我把它放在我的初始化中:
window.requests = []
$.ajaxSetup({
beforeSend: function(xhr){
var new_request_list = [xhr]
$.each(window.requests, function(k,v){
if(v.readyState != 4) new_request_list.push(v)
})
window.requests = new_request_list
}
})
I put in the snippet with new_request_list
just to keep the global variable from getting crowded with completed XHR objects.
我在new_request_list中添加了片段,以防止全局变量被完整的XHR对象挤占。
#4
2
XMLHttpRequest has an abort() function. $.ajax lets you get your hands on the xhr object. keep a record of all outstanding xhr's and go xhr.abort()
for any you want to terminate.
XMLHttpRequest有一个abort()函数。 $ .ajax让您可以轻松使用xhr对象。保留所有未完成的xhr的记录,并为任何想要终止的人输入xhr.abort()。
#5
1
I would say store all ajax requests in an array. When you need to kill then just loop through all values in array and call abort(). When a request is completed or aborted just pop from the array.
我想说将所有ajax请求存储在一个数组中。当你需要kill时,只需遍历数组中的所有值并调用abort()。当请求完成或中止时,只需从数组中弹出。
#6
1
I found the following on jsfiddle. It is mostly the same as above, except it will remove each request once they are complete. The solutions above just keep adding to the array. So over time, it could get huge if you are making a lot of ajax calls. You call $.xhrPool.abortAll();
when you want to abort all outstanding requests.
我在jsfiddle上找到了以下内容。它与上面大致相同,只要它们在完成后删除每个请求。上面的解决方案只是不断添加到阵列。所以随着时间的推移,如果你正在进行大量的ajax调用,它可能会变得很大。你打电话给$ .xhrPool.abortAll();当你想要中止所有未完成的请求时。
// $.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);
}
}
});