3

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!

burakakkor
  • 335
  • 3
  • 12

2 Answers2

8

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();
   });
}
adeneo
  • 312,895
  • 29
  • 395
  • 388
  • i got 11 ajax calls this works well on first 8 calls but i got error at 9th and want to cancel the other 2 calls and thats the thing not working any idea? :) – burakakkor Jul 23 '12 at 10:09
  • also checked to length of the array when i got the error at 9th is 8. and the other 2 calls working async. they triggered when the 9th finished or got error. – burakakkor Jul 23 '12 at 10:10
  • [link](http://img198.imageshack.us/img198/920/ajaxsy.jpg) and this is the all of my ajax calls as you can see i got the error and thats what i want but the other 2 just triggered when the 8th finished or got error :( – burakakkor Jul 23 '12 at 10:14
3

Every time you create an ajax request it returns an object, you can use it to abort the request:

var request = $.ajax({
    type: 'POST',
    url: url,
    success: function(data){}
});

and then use the request to abort the call:

request.abort();
Tomer
  • 17,787
  • 15
  • 78
  • 137