简体   繁体   中英

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. It is updated every 5 seconds.So many pending ajax requests will be there.

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.

I tried to use

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

But it is aborting only the last ajax call. If anyone has idea or any working examples to abort all the ajax calls, please help me!! Thanks a lot in advance!!

Define xhrPool = [] outside of fn function. xhrPool is redefined at each call to fn when setInterval is called. function abortAjax() should be 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();
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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