Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery abandon all outstanding AJAX requests

Tags:

jquery

ajax

Is there any easy way to abandon all outstanding jQuery AJAX requests? My application is capable of pulling off a lot of simultaneous requests, so race becomes problematic. I've put in hackish solutions (namely, putting in a flag that is checked upon request completion), but it would be much nicer to get a global stop all outstanding requests function.

like image 540
Steven Avatar asked Jul 22 '10 20:07

Steven


2 Answers

Assign every ajax request as an element of an array:

var requests = [];

requests.push($.ajax({
    type: 'POST',
    url: '...',
    success: successfunc
    });
);

And to kill:

$.each(requests, function(i, v) {
    v.abort();
});
like image 156
Ken Redler Avatar answered Oct 31 '22 19:10

Ken Redler


as the others have said, use the .abort() method. However, this ONLY works on calls within the same domain. once you enter into cross-site calls (with jsonp), then the .abort() method is delegated to null, so wont actually fire/work.

worth noting as this caused me endless debugging time many a small moon ago :)

jim

like image 3
jim tollan Avatar answered Oct 31 '22 20:10

jim tollan