Stop all active ajax requests in jQuery

前端 未结 16 904
自闭症患者
自闭症患者 2020-11-22 13:51

I have a problem, when submitting a form all active ajax request fail, and that triggers error event.

How to stop all active ajax requests in jQuery without trigerri

相关标签:
16条回答
  • 2020-11-22 14:09

    I extended mkmurray and SpYk3HH answer above so that xhrPool.abortAll can abort all pending requests of a given url :

    $.xhrPool = [];
    $.xhrPool.abortAll = function(url) {
        $(this).each(function(i, jqXHR) { //  cycle through list of recorded connection
            console.log('xhrPool.abortAll ' + jqXHR.requestURL);
            if (!url || url === jqXHR.requestURL) {
                jqXHR.abort(); //  aborts connection
                $.xhrPool.splice(i, 1); //  removes from list by index
            }
        });
    };
    $.ajaxSetup({
        beforeSend: function(jqXHR) {
            $.xhrPool.push(jqXHR); //  add connection to list
        },
        complete: function(jqXHR) {
            var i = $.xhrPool.indexOf(jqXHR); //  get index for current connection completed
            if (i > -1) $.xhrPool.splice(i, 1); //  removes from list by index
        }
    });
    $.ajaxPrefilter(function(options, originalOptions, jqXHR) {
        console.log('ajaxPrefilter ' + options.url);
        jqXHR.requestURL = options.url;
    });
    

    Usage is same except that abortAll can now optionally accept a url as a parameter and will cancel only pending calls to that url

    0 讨论(0)
  • 2020-11-22 14:10

    There is a dummy solution I use it to abort all ajax requests. This solution is reload the whole page. This solution is good if you don't like to assign an ID to each ajax request, and if you make ajax requests inside for-loop. This will make sure all ajax requests are killed.

    location.reload();
    
    0 讨论(0)
  • 2020-11-22 14:11

    Here's what I'm currently using to accomplish that.

    $.xhrPool = [];
    $.xhrPool.abortAll = function() {
      _.each(this, function(jqXHR) {
        jqXHR.abort();
      });
    };
    $.ajaxSetup({
      beforeSend: function(jqXHR) {
        $.xhrPool.push(jqXHR);
      }
    });
    

    Note: _.each of underscore.js is present, but obviously not necessary. I'm just lazy and I don't want to change it to $.each(). 8P

    0 讨论(0)
  • 2020-11-22 14:13

    Make a pool of all ajax request and abort them.....

    var xhrQueue = []; 
    
    $(document).ajaxSend(function(event,jqxhr,settings){
        xhrQueue.push(jqxhr); //alert(settings.url);
    });
    
    $(document).ajaxComplete(function(event,jqxhr,settings){
        var i;   
        if((i=$.inArray(jqxhr,xhrQueue)) > -1){
            xhrQueue.splice(i,1); //alert("C:"+settings.url);
        }
    });
    
    ajaxAbort = function (){  //alert("abortStart");
        var i=0;
        while(xhrQueue.length){ 
            xhrQueue[i++] .abort(); //alert(i+":"+xhrQueue[i++]);
        }
    };
    
    0 讨论(0)
  • 2020-11-22 14:17

    I found it too easy for multiple requests.

    step1: define a variable at top of page:

      xhrPool = []; // no need to use **var**
    

    step2: set beforeSend in all ajax requests:

      $.ajax({
       ...
       beforeSend: function (jqXHR, settings) {
            xhrPool.push(jqXHR);
        },
        ...
    

    step3: use it whereever you required:

       $.each(xhrPool, function(idx, jqXHR) {
              jqXHR.abort();
        });
    
    0 讨论(0)
  • 2020-11-22 14:18

    The following snippet allows you to maintain a list (pool) of request and abort them all if needed. Best to place in the <HEAD> of your html, before any other AJAX calls are made.

    <script type="text/javascript">
        $(function() {
            $.xhrPool = [];
            $.xhrPool.abortAll = function() {
                $(this).each(function(i, jqXHR) {   //  cycle through list of recorded connection
                    jqXHR.abort();  //  aborts connection
                    $.xhrPool.splice(i, 1); //  removes from list by index
                });
            }
            $.ajaxSetup({
                beforeSend: function(jqXHR) { $.xhrPool.push(jqXHR); }, //  annd connection to list
                complete: function(jqXHR) {
                    var i = $.xhrPool.indexOf(jqXHR);   //  get index for current connection completed
                    if (i > -1) $.xhrPool.splice(i, 1); //  removes from list by index
                }
            });
        })
    </script>
    
    0 讨论(0)
提交回复
热议问题