How to cancel/abort an Ajax request already happened, but that I have not yet received the response from?

0 votes
943 views
asked Jul 28 in Jquery by Suni

Using jQuery, how can I cancel/abort an Ajax request that I have not yet received the response from?

I've an AJAX request which will be made every x seconds. But the problem is before the AJAX request if the previous request is not completed I've to abort that request and make a new request.

1 Answer

0 votes
answered Jul 28 by Jonny
jQuery Ajax methods return an XMLHttpRequest (or the equivalent) object, so you can just use abort()

$(document).ready(
    var xhr;

    var fn = function(){
        if(xhr && xhr.readyState != 4){
            xhr.abort();
        }
        xhr = $.ajax({
            url: 'ajax/progress.ftl',
            success: function(data) {
                //do something
            }
        });
    };

    var interval = setInterval(fn, 500);
);

Related questions

+2 votes
2 answers 4083 views
+1 vote
1 answer 236 views
0 votes
1 answer 385 views
0 votes
1 answer 434 views
0 votes
0 answers 239 views
...