0

Possible Duplicate:
Stop all active ajax requests in jQuery

I want to have a button that will abort any ajax requests currently being run. Below is a selector that grabs the right button. What should go in the place of the alert('test')?

$('#abort').click(function(){
     alert('test')
   });
Community
  • 1
  • 1
Spencer
  • 21,348
  • 34
  • 85
  • 121
  • 1
    You can find your answer here: http://stackoverflow.com/questions/446594/kill-ajax-requests-using-javascript-using-jquery/446626 – Stuiterbal Oct 14 '11 at 17:25

2 Answers2

3

You need to call abort() method - on each of the ajax calls !

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

After that you can abort the request:

request.abort();
Royi Namir
  • 144,742
  • 138
  • 468
  • 792
0

Create an array for each xhr object:

var xhrArray = new Array();

For every xhr object you need to add it to an array (where xhr is the object):

xhrArray.push(xhr);

Then on abort:

for( i = 0; i < xhrArray.length; i++){
        xhrArray[i].abort();
        stop = true;
    }

The stop variable is for a loop that is elsewhere in my code to prevent more xhr objects from being created/sent.

James
  • 539
  • 2
  • 6