开发者

Jquery On Click Function that Stops an Ajax Requests currently running [duplicate]

开发者 https://www.devze.com 2023-04-13 06:37 出处:网络
This question already has answers here: 开发者_如何学GoClosed 11 years ago. Possible Duplicate: Stop all active ajax requests in jQuery
This question already has answers here: 开发者_如何学Go Closed 11 years ago.

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')
   });


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();


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消