0 votes
11.4k views
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

+1 vote
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 9.7k views
+1 vote
1 answer 1.2k views
0 votes
1 answer 1.8k views
0 votes
1 answer 1.8k views
+1 vote
1 answer 3.8k views
...