回答
每当你创建你可以使用一个变量来存储它一个Ajax请求:
var request = $.ajax({
type: 'POST',
url: 'someurl',
success: function(result){}
});
然后你就可以中止请求:
request.abort();
你可以使用一个数组保存所有的曲目等待ajax请求并在必要时中止它们。
这是我目前用来完成的。
$.xhrPool = [];
$.xhrPool.abortAll = function() {
_.each(this, function(jqXHR) {
jqXHR.abort();
});
};
$.ajaxSetup({
beforeSend: function(jqXHR) {
$.xhrPool.push(jqXHR);
}
});
注:_each of underscore.js存在,但显然不是必需的。我只是懒惰,我不想将它改为$ .each()。 8P
我和安迪的代码存在一些问题,但它给了我一些很棒的点子。首先问题是我们应该弹出成功完成的任何jqXHR对象。我也必须修改abortAll函数。这是我的最终工作代码:
$.xhrPool = [];
$.xhrPool.abortAll = function() {
$(this).each(function(idx, jqXHR) {
jqXHR.abort();
});
};
$.ajaxSetup({
beforeSend: function(jqXHR) {
$.xhrPool.push(jqXHR);
}
});
$(document).ajaxComplete(function() {
$.xhrPool.pop();
});
我不喜欢ajaxComplete()方法做事情。无论我如何尝试配置.ajaxSetup,它都不起作用。
以下代码片段允许您维护请求的列表(池),并在需要时将它们全部中止。最好放置在您的html的<HEAD>
之前,之前任何其他的AJAX调用。
<script type="text/javascript">
$(function() {
$.xhrPool = [];
$.xhrPool.abortAll = function() {
$(this).each(function(i, jqXHR) { // cycle through list of recorded connection
jqXHR.abort(); // aborts connection
$.xhrPool.splice(i, 1); // removes from list by index
});
}
$.ajaxSetup({
beforeSend: function(jqXHR) { $.xhrPool.push(jqXHR); }, // annd connection to list
complete: function(jqXHR) {
var i = $.xhrPool.indexOf(jqXHR); // get index for current connection completed
if (i > -1) $.xhrPool.splice(i, 1); // removes from list by index
}
});
})
</script>
你钉了它。谢谢 – Trip 2012-04-13 09:03:16
@mkmurray - 关于在IE8 initalization我似乎得到'对象不支持属性或方法'indexOf''?我怀疑它可能是http://stackoverflow.com/a/2608601/181971或者只是交换到http://stackoverflow.com/a/2608618/181971? – Tim 2012-05-09 05:26:20
@grr是正确的,请参阅他的答案并检查[ajaxSetup](http://api.jquery.com/jQuery.ajaxSetup/)的文档。 – kzfabi 2013-08-13 14:14:21
使用ajaxSetup is not correct,因为这是它的文档页面上注明。它只设置默认值,如果有一些请求覆盖它们,会有一些混乱。
我很晚参加聚会,但只是为了将来的参考,如果有人正在寻找解决相同的问题,这里是我的去,它的灵感和大体相同的以前的答案,但更完整
// 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);
比从未更好的迟到,伟大的工作! – 2012-07-12 10:19:08
好工作,帮助了我! – devmonster 2012-09-23 08:58:03
如何称呼终止()方法从其他函数中调用 – 2015-09-30 21:28:58
为每个xhr请求提供一个唯一的id,并在发送前将对象引用存储在对象中。 xhr请求完成后删除引用。
要取消所有请求的任何时间:
$.ajaxQ.abortAll();
返回取消的请求的唯一的ID。仅用于测试目的。
工作函数:
$.ajaxQ = (function(){
var id = 0, Q = {};
$(document).ajaxSend(function(e, jqx){
jqx._id = ++id;
Q[jqx._id] = jqx;
});
$(document).ajaxComplete(function(e, jqx){
delete Q[jqx._id];
});
return {
abortAll: function(){
var r = [];
$.each(Q, function(i, jqx){
r.push(jqx._id);
jqx.abort();
});
return r;
}
};
})();
返回与可用于在需要时添加更多的功能单一的功能的对象。
同样重要的是:假设你想注销,并且你正在用计时器产生新的请求:因为会话数据每更新一次引导程序(也许你可以告诉我在说Drupal,但这可能是任何使用会议)......我不得不去通过我的所有脚本与搜索和替换,因为我曾在不同的情况下运行的一吨的东西:在顶部的全局变量:
var ajReq = [];
var canAj = true;
function abort_all(){
for(x in ajReq){
ajReq[x].abort();
ajReq.splice(x, 1)
}
canAj = false;
}
function rmvReq(ranNum){
var temp = [];
var i = 0;
for(x in ajReq){
if(x == ranNum){
ajReq[x].abort();
ajReq.splice(x, 1);
}
i++;
}
}
function randReqIndx(){
if(!canAj){ return 0; }
return Math.random()*1000;
}
function getReqIndx(){
var ranNum;
if(ajReq.length){
while(!ranNum){
ranNum = randReqIndx();
for(x in ajReq){
if(x===ranNum){
ranNum = null;
}
}
}
return ranMum;
}
return randReqIndx();
}
$(document).ready(function(){
$("a").each(function(){
if($(this).attr('href').indexOf('/logout')!=-1){
$(this).click(function(){
abort_all();
});
}
})
});
// Then in all of my scripts I wrapped my ajax calls... If anyone has a suggestion for a
// global way to do this, please post
var reqIndx = getReqIndx();
if(reqIndx!=0){
ajReq[reqIndx] = $.post(ajax, { 'action': 'update_quantities', iids:iidstr, qtys:qtystr },
function(data){
//..do stuff
rmvReq(reqIndx);
},'json');
}
我的代码更新使它适用于我
$.xhrPool = [];
$.xhrPool.abortAll = function() {
$(this).each(function(idx, jqXHR) {
jqXHR.abort();
});
$(this).each(function(idx, jqXHR) {
var index = $.inArray(jqXHR, $.xhrPool);
if (index > -1) {
$.xhrPool.splice(index, 1);
}
});
};
$.ajaxSetup({
beforeSend: function(jqXHR) {
$.xhrPool.push(jqXHR);
},
complete: function(jqXHR) {
var index = $.inArray(jqXHR, $.xhrPool);
if (index > -1) {
$.xhrPool.splice(index, 1);
}
}
});
以下是如何挂钩(如果您的页面正在放置多个AJAX调用,并且您试图导航离开,则很有用)。
$ ->
$.xhrPool = [];
$(document).ajaxSend (e, jqXHR, options) ->
$.xhrPool.push(jqXHR)
$(document).ajaxComplete (e, jqXHR, options) ->
$.xhrPool = $.grep($.xhrPool, (x) -> return x != jqXHR);
$(document).delegate 'a', 'click', ->
while (request = $.xhrPool.pop())
request.abort()
我伸出mkmurray和SpYk3HH答案的上方,使得xhrPool.abortAll可以中止所有等待的请求指定网址的:
$.xhrPool = [];
$.xhrPool.abortAll = function(url) {
$(this).each(function(i, jqXHR) { // cycle through list of recorded connection
console.log('xhrPool.abortAll ' + jqXHR.requestURL);
if (!url || url === jqXHR.requestURL) {
jqXHR.abort(); // aborts connection
$.xhrPool.splice(i, 1); // removes from list by index
}
});
};
$.ajaxSetup({
beforeSend: function(jqXHR) {
$.xhrPool.push(jqXHR); // add connection to list
},
complete: function(jqXHR) {
var i = $.xhrPool.indexOf(jqXHR); // get index for current connection completed
if (i > -1) $.xhrPool.splice(i, 1); // removes from list by index
}
});
$.ajaxPrefilter(function(options, originalOptions, jqXHR) {
console.log('ajaxPrefilter ' + options.url);
jqXHR.requestURL = options.url;
});
用法是,除了abortAll现在可以选择接受网址相同作为参数,并且将只取消对该URL的未决呼叫
投掷我的帽子。提供abort
和remove
针对xhrPool
arr的方法ay,并且不容易出现ajaxSetup
覆盖问题。
/**
* Ajax Request Pool
*
* @author Oliver Nassar <[email protected]>
* @see http://stackoverflow.com/questions/1802936/stop-all-active-ajax-requests-in-jquery
*/
jQuery.xhrPool = [];
/**
* jQuery.xhrPool.abortAll
*
* Retrieves all the outbound requests from the array (since the array is going
* to be modified as requests are aborted), and then loops over each of them to
* perform the abortion. Doing so will trigger the ajaxComplete event against
* the document, which will remove the request from the pool-array.
*
* @access public
* @return void
*/
jQuery.xhrPool.abortAll = function() {
var requests = [];
for (var index in this) {
if (isFinite(index) === true) {
requests.push(this[index]);
}
}
for (index in requests) {
requests[index].abort();
}
};
/**
* jQuery.xhrPool.remove
*
* Loops over the requests, removes it once (and if) found, and then breaks out
* of the loop (since nothing else to do).
*
* @access public
* @param Object jqXHR
* @return void
*/
jQuery.xhrPool.remove = function(jqXHR) {
for (var index in this) {
if (this[index] === jqXHR) {
jQuery.xhrPool.splice(index, 1);
break;
}
}
};
/**
* Below events are attached to the document rather than defined the ajaxSetup
* to prevent possibly being overridden elsewhere (presumably by accident).
*/
$(document).ajaxSend(function(event, jqXHR, options) {
jQuery.xhrPool.push(jqXHR);
});
$(document).ajaxComplete(function(event, jqXHR, options) {
jQuery.xhrPool.remove(jqXHR);
});
最好使用独立的代码.....
var xhrQueue = [];
$(document).ajaxSend(function(event,jqxhr,settings){
xhrQueue.push(jqxhr); //alert(settings.url);
});
$(document).ajaxComplete(function(event,jqxhr,settings){
var i;
if((i=$.inArray(jqxhr,xhrQueue)) > -1){
xhrQueue.splice(i,1); //alert("C:"+settings.url);
}
});
ajaxAbort = function(){ //alert("abortStart");
var i=0;
while(xhrQueue.length){
xhrQueue[i++] .abort(); //alert(i+":"+xhrQueue[i++]);
}
};
让所有的Ajax请求的游泳池和中止他们.....
var xhrQueue = [];
$(document).ajaxSend(function(event,jqxhr,settings){
xhrQueue.push(jqxhr); //alert(settings.url);
});
$(document).ajaxComplete(function(event,jqxhr,settings){
var i;
if((i=$.inArray(jqxhr,xhrQueue)) > -1){
xhrQueue.splice(i,1); //alert("C:"+settings.url);
}
});
ajaxAbort = function(){ //alert("abortStart");
var i=0;
while(xhrQueue.length){
xhrQueue[i++] .abort(); //alert(i+":"+xhrQueue[i++]);
}
};
我觉得太容易多个请求。
第一步:在页面顶部定义变量:
xhrPool = []; // no need to use **var**
第二步:集beforeSend在所有Ajax请求:
$.ajax({
...
beforeSend: function (jqXHR, settings) {
xhrPool.push(jqXHR);
},
...
第三步:等。无论您需要使用它:
$.each(xhrPool, function(idx, jqXHR) {
jqXHR.abort();
});
这是按原样! – 2017-06-22 19:51:52
我只是想:) – 2017-10-28 05:19:10
这会泄漏内存,正如https://stackoverflow.com/a/6618288/1772379所做的那样,并且出于完全相同的原因。 – 2017-11-08 22:13:48
- 1. 全局中止所有jQuery AJAX请求
- 2. AJAX请求没有停止
- 3. 在ajaxStart中停止jquery ajax请求
- 4. 捕获jQuery中的所有ajax请求
- 5. 停止射击Ajax请求
- 6. 如何停止Ajax请求
- 7. 即使所有先前的请求都中止,jQuery AJAX请求也会变慢
- 8. 如何停止ajaxSend中的ajax请求?
- 9. 所有命令链接形式停止工作AJAX请求后
- 10. jQuery:在Chrome中的Ajax请求中止
- 11. Ajax请求中的jQuery Ajax请求
- 12. 取消活动ajax请求
- 13. 当有多个保持活动请求时HttpWebRequest停止
- 14. 中止AJAX请求
- 15. 如何中止jQuery 3.0 AJAX请求?
- 16. 在Ajax请求后setInterval停止
- 17. jquery ajax onkeystroke停止动画?
- 18. jQuery AJAX请求在长请求中中止后重新发送
- 19. jquery .ajax请求被长时间运行阻止.ajax请求
- 20. 所有AJAX请求完成?
- 21. AJAX请求(所有版本)
- 22. 中止活动图像请求
- 23. 如何使用jquery中止所有挂起的ajax请求的列表?
- 24. NodeJS请求模块在6个Ajax请求后停止工作
- 25. jQuery中止请求
- 26. 停止发送没有页面刷新的Ajax请求
- 27. jquery AJAX请求
- 28. 如何停止重复请求jquery?
- 29. 如何停止jQuery长轮询请求?
- 30. jquery ajax请求中的URL
是的,是的,http://plugins.jquery.com/project/ajaxqueue可以提供帮助。 – umpirsky 2009-11-27 12:00:45
THXs,我添加了一个标志,因为我同时使用了多个请求 – jcho360 2012-09-19 14:56:54
这里是简单的工作示例:http://stackoverflow.com/a/42312101/3818394 – 2017-02-18 06:53:54