简体   繁体   English

在发送另一个之前,jquery abort()ajax请求

[英]jquery abort() ajax request before sending another

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? 基于: 使用jQuery中止Ajax请求 ...在inventory_search()中 - 在发出ajax请求之前,如何检查当前请求并在发出新请求之前中止()它们? Or... Is there a better way to do this? 或者......有更好的方法吗?

<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>

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. 然后,如果您找到需要中止所有现有请求的点,则可以循环遍历该数组并对所有挂起的请求调用abort。 Should be pretty simple. 应该很简单。

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? 编辑:检查这个问题类似的情况: 如何避免3 ajax调用?

EDIT 2: So I what you can do is have an array that you append all your ajax calls to. 编辑2:所以我可以做的是有一个数组,你附加所有的ajax调用。 Which is essentially just making an XmlHttpRequest and that is what is returned from the ajax call. 这实际上只是制作一个XmlHttpRequest,这是从ajax调用返回的内容。 So 所以

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. 希望这足以让你开始。

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);
        }
    });
}

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) {}});

Use a global variable to hold the handle of the current request. 使用全局变量来保存当前请求的句柄。 In this case declare the var a out side the function and make it global. 在这种情况下,将var a out声明为函数并使其成为全局函数。 Before calling the ajax check it is not null. 在调用ajax检查之前,它不是null。 If it is not null abort it other wise assign it with new ajax. 如果它不是空的abort那么另外明智地用新的ajax分配它。 In the success/complete event make sure you clear the variable a. 在success / complete事件中,请确保清除变量a。 May not be best solution out there, but works best. 可能不是最好的解决方案,但效果最好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM