Search code examples
jqueryajaxbreakjqxhr

Aborting All Ajax Calls - jqXHR


Basically im using jqXHR with asmx. I want to do this if its possible;

Overally in everypage im using 6-7 ajax calls with sync or async depends on which method it is. But when one of them got error i want to break that ajax call and after the all of ajax calls.

Thanks already!


Solution

  • use an array to store the returned object from each ajax call, and iterate over that array to abort them all:

    var XHR = []
    ........//later
    var myajax = $.ajax({
        type: 'GET',
        url: url
    }).done(function() {
        //do something
    }).fail(function() {
       abortAll();
    });
    XHR.push(myajax);
    
    function abortAll() {
       XHR.each(function(i,e) {
          e.abort();
       });
    }