5

I would like to stop an ajax call in jquery, which is not working

var xhr = null;

xhr = $.ajax({
    url : 'www.example.com?some-large-call',
    success : function(responseText) {
        // some DOM manipulation
    }
});

$("#button").click(function() { xhr.abort() });

I referred the below link

http://www.stoimen.com/blog/2009/09/29/jquery-stop-an-ajax-call/

hjgraca
  • 1,695
  • 1
  • 14
  • 29
user3375068
  • 61
  • 1
  • 2

3 Answers3

3

this probally has more XHR..

Please see this answer:

jquery abort() ajax request before sending another

From the link:

every time you do ajax request, add to array:

requests.push(
$.ajax({
    type: 'post',
    url: '/test.php',
    data: search_data,
    success: function(data) {
        $('#catalog').html(data);
    }
}));

Then, when you want to stop calls.. loop through array and abort all calls..

 for(var i = 0; i < requests.length; i++)
requests[i].abort();

Thanks

Community
  • 1
  • 1
JF it
  • 2,403
  • 3
  • 20
  • 30
3

xhr.abort() it will cause the client to stop listening for the event, but probably it may not stop the server from processing it.

Sumeet Kumar Yadav
  • 11,912
  • 6
  • 43
  • 80
0

Provided the code you've pasted is the code you're actually using it's no wonder it doesn't work.

As it states on the site it is just pseudo code to illustrate an example. There is no AJAX request there and nothing to stop (at least nothing large enough to be able to stop)

BenM
  • 4,218
  • 2
  • 31
  • 58