中止先前运行的Ajax请求

Dil*_*eni 8 javascript ajax jquery

是否可以中止以前运行的Ajax请求?

var xhr = $.ajax({
    type: "POST",
    url: "some.php",
    data: "name=John&location=Boston",
    success: function(msg){
       alert( "Data Saved: " + msg );
    }
});
Run Code Online (Sandbox Code Playgroud)

raj*_*wat 5

尝试这样的事情

        $(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 );
                    }
                });
            }
        });
Run Code Online (Sandbox Code Playgroud)