中止先前运行的Ajax请求

时间:2013-11-23 05:41:46

标签: javascript jquery ajax

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

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

1 个答案:

答案 0 :(得分: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 );
                    }
                });
            }
        });