Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Abort all jQuery AJAX requests globally

Is there a way to abort all Ajax requests globally without a handle on the request object?

The reason I ask is that we have quite a complex application where we are running a number of different Ajax requests in the background by using setTimeOut(). If the user clicks a certain button we need to halt all ongoing requests.

like image 226
Robert W Avatar asked Aug 11 '10 08:08

Robert W


People also ask

Which method can be called to cancel the XMLHttpRequest in AJAX?

The XMLHttpRequest. abort() method aborts the request if it has already been sent.

Which method is used to cancel current AJAX?

Overview. We may use the abort() method to cancel a sent AJAX request.

How do I know if AJAX request is complete?

The ajaxStop() method specifies a function to run when ALL AJAX requests have completed. When an AJAX request completes, jQuery checks if there are any more AJAX requests. The function specified with the ajaxStop() method will run if no other requests are pending.


2 Answers

You need to call abort() method:

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

After that you can abort the request:

request.abort();

This way you need to create a variable for your ajax request and then you can use the abort method on that to abort the request any time.

Also have a look at:

  • Aborting Ajax
like image 84
Sarfraz Avatar answered Oct 04 '22 08:10

Sarfraz


You cannot abort all active Ajax requests if you are not tracking the handles to them.

But if you are tracking it, then yes you can do it, by looping through your handlers and calling .abort() on each one.

like image 21
7wp Avatar answered Oct 04 '22 10:10

7wp