0 votes
5.2k views
asked in Jquery by

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 by
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 5k views
+1 vote
1 answer 292 views
0 votes
2 answers 10 views
0 votes
1 answer 419 views
0 votes
1 answer 520 views
...