简体   繁体   English

如何使用jQuery中止所有未决的ajax请求列表?

[英]How to abort list of all the pending ajax requests using jquery?

This is my function 这是我的职责

  var xhr;
    function abortAjax() {
        $.each(xhrPool, function(idx, jqXHR) {
            jqXHR.abort();
        });
    }
    $(document).ready(function() {
        fn = function() {
            xhrPool = [];
            xhr = $.ajax({
                url: '/getUrl',
                beforeSend: function(jqXHR) {
                    xhrPool.push(jqXHR);
                },
                complete: function(jqXHR, data) {
                    if (jqXHR.statusText != "error") {
                        //my functions
                    }
                }
            });
        };

        var interval = setInterval(fn, 5000);
    });

    function abort_allAjax(){     
      //I need to make fresh ajax call when it enters this function      
        function abortAjax();
        fn();
    }

fn is my function for updating table data. fn是我更新表数据的功能。 It is updated every 5 seconds.So many pending ajax requests will be there. 它每5秒更新一次,因此会有许多待处理的ajax请求。

When i call abort_allAjax() function, new data needs to be updated, so i need to abort all the pending requests and make a fresh ajax call. 当我调用abort_allAjax()函数时,需要更新新数据,因此我需要中止所有未决请求并进行新的ajax调用。

I tried to use 我尝试使用

 function abortAjax() {
        $.each(xhrPool, function(idx, jqXHR) {
            jqXHR.abort();
        });
    }

But it is aborting only the last ajax call. 但是它仅中止最后一个ajax调用。 If anyone has idea or any working examples to abort all the ajax calls, please help me!! 如果有人有想法或任何可行的示例来中止所有的ajax调用,请帮助我! Thanks a lot in advance!! 在此先多谢!!

Define xhrPool = [] outside of fn function. fn函数之外定义xhrPool = [] xhrPool is redefined at each call to fn when setInterval is called. 调用setInterval时,每次对fn调用都会重新定义xhrPool function abortAjax() should be abortAjax() . function abortAjax()应该是abortAjax()

I modified like below and it works 我像下面这样修改并且有效

        abortValue = false;
        var xhr;
        xhrPool = [];
        var trying;

        function abortAjax() {
            $.each(xhrPool, function(idx, jqXHR) {
                jqXHR.abort();

            });

        }
        $(document).ready(function() {
            fn = function() {
                xhr = $.ajax({
                    url: '/getUrl',
                    beforeSend: function(jqXHR) {
                        xhrPool.push(jqXHR);
                    },
                    complete: function(jqXHR, data) {
                        if (abortValue == true) {
                            abortAjax()
                        } else {
                            if (jqXHR.statusText != "error" && "undefined") {
                                //myactions

                            }

                        }
                    }
                });

            };

            var interval = setInterval(fn, 5000);
        });


        function updateData_function {
            //I want to abort all previous ajax calls and make a new ajax call since it will update the data

            abortValue = true;
            abortAjax();
            abortValue = false;
            fn();
    }

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

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