0

I have an Ajax function which takes some time to respond, due to large file. How do I interrupt the response function if I want to stop the Ajax request?

$("#startAjaxButton").click(function(){
  $.ajax({
    url:"ajaxRequest.txt",
      success:function(result){
         $("#div1").html(result);
      }
  });
});
PMG
  • 190
  • 1
  • 8
  • your question is answered here http://stackoverflow.com/questions/1802936/stop-all-active-ajax-requests-in-jquery – Nick Dec 21 '13 at 18:17
  • Alternatively, use `timeout` option within the `.ajax({})` object if it's over a specific time limit – MackieeE Dec 21 '13 at 18:18

3 Answers3

2

Just use the abort() method.

Like this:

var query = $.ajax({ ... });

// later...
query.abort();
Paul Rad
  • 4,820
  • 1
  • 22
  • 23
1

You can stop the ajax request by using the .abort(),

 var xRequest = $.ajax({
    url:"ajaxRequest.txt",
      success:function(result){
         $("#div1").html(result);
      }
  });

 xRequest.abort();
Rajaprabhu Aravindasamy
  • 66,513
  • 17
  • 101
  • 130
0

There are two ways to stop the ajax request. Either you can use the abort() method like following

var xhr = jQuery.ajax({
 url : "/test",
 ...
});

   xhr.abort();

Or using the timeout option like following,

 - var xhr = jQuery.ajax({
        timeout : 3000,
        ...
       }

Also you can refer the following links, which are similar to this question

abort-ajax-requests-using-jquery

How-to-Abort-a-AJAX-Call-Before-Calling-the-Same-A

Community
  • 1
  • 1
Ashwin Preetham Lobo
  • 1,856
  • 1
  • 14
  • 19