Javascript jquery abort() ajax 请求,然后再发送另一个请求
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3312960/
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
jquery abort() ajax request before sending another
提问by mike
Based on: Abort Ajax requests using jQuery... In inventory_search() - before the ajax request is made, how can I check for any current requests and abort() them before making a new request? Or... Is there a better way to do this?
基于:使用 jQuery 中止 Ajax 请求......在inventory_search() 中 - 在发出 ajax 请求之前,如何在发出新请求之前检查任何当前请求并中止它们?或者......有没有更好的方法来做到这一点?
<script type="text/javascript">
$(function() {
$('form#internal_catalog').change(function() {
inventory_search();
});
});
function inventory_search() {
var search_data = $('form#internal_catalog').serialize();
var a = $.ajax({
type: 'post',
url: '/test.php',
data: search_data,
success: function(data) {
$('#catalog').html(data);
}
});
}
</script>
回答by spinon
Create an array queue of all your requests. Then if you find a point where you need to abort all existing requests you can just loop through the array and call abort on all pending requests. Should be pretty simple.
创建所有请求的数组队列。然后,如果您发现需要中止所有现有请求的点,您可以循环遍历数组并对所有未决请求调用 abort。应该很简单。
Though the other way is to just keep an internal flag that indicates whether a request is currently in process and skip the request if there is one. Or handle how you see fit.
另一种方法是保留一个内部标志,指示当前是否正在处理请求,如果有则跳过该请求。或者处理你认为合适的方式。
EDIT: Check this SO question for a similar situation: How to avoid 3 ajax calls?
编辑:在类似情况下检查这个 SO 问题:How to avoid 3 ajax calls?
EDIT 2: So I what you can do is have an array that you append all your ajax calls to. Which is essentially just making an XmlHttpRequest and that is what is returned from the ajax call. So
编辑 2:所以我你可以做的是有一个数组,你将所有的 ajax 调用附加到其中。这本质上只是创建一个 XmlHttpRequest,这就是从 ajax 调用返回的内容。所以
requests.push(
$.ajax({
type: 'post',
url: '/test.php',
data: search_data,
success: function(data) {
$('#catalog').html(data);
}
}));
This will add all your requests to a requests array which you can define somewhere. Then when you want to kill all pending requests you can just loop through the array and call abort which will kill the request.
这会将您的所有请求添加到您可以在某处定义的请求数组中。然后当你想终止所有挂起的请求时,你可以循环遍历数组并调用 abort 来终止请求。
for(var i = 0; i < requests.length; i++)
requests[i].abort();
Or just define a variable outside of your function that is a flag indicating whether a request is being made. You can even make it more specific and store the search data and only skip requests that have a pending request for the same data and allow other requests that are for different data.
或者只是在您的函数之外定义一个变量,它是一个标志,指示是否正在发出请求。您甚至可以使其更具体并存储搜索数据,并且仅跳过对相同数据有待处理请求的请求,并允许针对不同数据的其他请求。
Hopefully that is enough to get you started.
希望这足以让你开始。
回答by Rob Forrest
Spinon's answer is great for aborting all ajax queries but not great for keeping track of just one.
Spinon 的答案非常适合中止所有 ajax 查询,但不适合仅跟踪一个。
I often find myself doing something like
我经常发现自己在做类似的事情
var ajax_request;
function do_something()
{
if(typeof ajax_request !== 'undefined')
ajax_request.abort();
ajax_request = $.ajax({
type: 'post',
url: '/test.php',
data: search_data,
success: function(data) {
$('#catalog').html(data);
}
});
}
回答by Komang
Pretty easy with jQuery AjaxManager Plugin:
使用jQuery AjaxManager 插件非常简单:
$.manageAjax.create('unique_identifier',{
queue:'clear',cacheResponse:false,maxRequests:1,abortOld:true
});
jQuery.manageAjax.abort('unique_identifier');
jQuery.manageAjax.clear('unique_identifier');
jQuery.manageAjax.add('unique_identifier',{success: function(data) {}});
回答by Teja Kantamneni
Use a global variable to hold the handle of the current request. In this case declare the var aout side the function and make it global. Before calling the ajax check it is not null. If it is not null abortit other wise assign it with new ajax. In the success/complete event make sure you clear the variable a. May not be best solution out there, but works best.
使用全局变量来保存当前请求的句柄。在这种情况下,声明var a函数的外部并使其成为全局的。在调用 ajax 检查之前它不为空。如果它不为空abort,则以其他方式为它分配新的 ajax。在成功/完成事件中,确保清除变量 a。可能不是最好的解决方案,但效果最好。

