Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Abort a previous running Ajax request [duplicate]

Is it possible to abort a previously-running Ajax request?

var xhr = $.ajax({
    type: "POST",
    url: "some.php",
    data: "name=John&location=Boston",
    success: function(msg){
       alert( "Data Saved: " + msg );
    }
});
like image 324
Dileep Kheni Avatar asked Nov 23 '13 05:11

Dileep Kheni


People also ask

How can I cancel old AJAX request?

Just use ajax.abort(); } //then you make another ajax request $. ajax( //your code here );

How do I stop multiple AJAX calls from repeated clicks?

}); If isLoading is false, the AJAX call starts, and we immediately change its value to true. Once the AJAX response is received, we turn the value of that variable back to false, so that we can stop ignoring new clicks.

Which method cancels the current request?

abort() method aborts the request if it has already been sent. When a request is aborted, its readyState is changed to XMLHttpRequest.

What is processData in AJAX?

processData: It's default value is true. It is used to specify whether or not data sent with the request should be transformed into a query string.


1 Answers

try something like this

        $(document).ready(function(){
            var xhr; // global object

            function your_function() {
                if(xhr && xhr.readystate != 4){
                    xhr.abort();
                }

                xhr = $.ajax({
                    type: "POST",
                    url: "some.php",
                    data: "name=John&location=Boston",
                    success: function(msg){
                       alert( "Data Saved: " + msg );
                    }
                });
            }
        });
like image 127
rajesh kakawat Avatar answered Sep 29 '22 14:09

rajesh kakawat