jQuery的中止()Ajax请求发送另一个前 [英] jquery abort() ajax request before sending another

查看:164
本文介绍了jQuery的中止()Ajax请求发送另一个前的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据<一href="http://stackoverflow.com/questions/446594/kill-ajax-requests-using-javascript-using-jquery">http://stackoverflow.com/questions/446594/kill-ajax-requests-using-javascript-using-jquery...在inventory_search() - Ajax请求作出之前,我怎么能检查任何当前的请求和中止()他们做一个新的请求之前?还是......有没有更好的方式来做到这一点?

 &LT;脚本类型=文/ JavaScript的&GT;

    $(函数(){
        $('#形式internal_catalog')。改变(函数(){
            inventory_search();
        });
    });

    功能inventory_search(){
        VAR search_data = $('#形式internal_catalog')序列化()。
        VAR一个= $阿贾克斯({
            类型:'后',
            网址:/test.php,
            数据:search_data,
            成功:功能(数据){
                $('#目录)HTML(数据)。
            }
        });
    }

&LT; / SCRIPT&GT;
 

解决方案

创建所有请求的数组队列。然后,如果你找到一个地步,你需要终止所有现有的请求中,可以通过阵列只是循环,并呼吁中止所有未决请求。应该是pretty的简单。

虽然另一种方法是只保留一个内部标志,指示请求是否是当前进程,并跳过请求,如果有一个。或者处理你怎么认为合适的。

编辑:检查这太问题进行了类似的情况:<一href="http://stackoverflow.com/questions/3251290/how-to-avoid-3-ajax-calls/3251360#3251360">http://stackoverflow.com/questions/3251290/how-to-avoid-3-ajax-calls/3251360#3251360

编辑2:那么我是你可以做的是有你追加所有Ajax调用数组。这实质上是刚刚作出XmlHtt prequest,这就是从AJAX调用返回。因此,

  requests.push(
    $阿贾克斯({
        类型:'后',
        网址:/test.php,
        数据:search_data,
        成功:功能(数据){
            $('#目录)HTML(数据)。
        }
    }));
 

这会增加你所有的请求到一个请求数组,你可以定义的地方。然后,当你想通过杀死阵列的所有未决请求你可以循环,并呼吁中止,这将杀死请求。

 的(VAR I = 0; I&LT; requests.length;我++)
    请求[I] .abort();
 

或者只是定义功能,这是一个标志,指示是否正在提出的要求之外的变量。甚至可以使其更加具体和存储搜索数据和只跳过具有对于相同的数据挂起请求的请求和允许那些对不同的数据的其他请求。

希望这是足以让你开始。

Based on: http://stackoverflow.com/questions/446594/kill-ajax-requests-using-javascript-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?

<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. 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: http://stackoverflow.com/questions/3251290/how-to-avoid-3-ajax-calls/3251360#3251360

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

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.

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.

这篇关于jQuery的中止()Ajax请求发送另一个前的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆