JQuery$.ajax
返回一个 xmlHttpRequest 对象,该对象可以通过其本机方法取消.abort()
$("#search").autocomplete({
minLength: 3,
delay: 300, // this is in milliseconds
json: true,
source: function(request, response){
// New request 300ms after key stroke
var $this = $(this);
var $element = $(this.element);
var previous_request = $element.data( "jqXHR" );
if( previous_request ) {
// a previous request has been made.
// though we don't know if it's concluded
// we can try and kill it in case it hasn't
previous_request.abort();
}
// Store new AJAX request
$element.data( "jqXHR", $.ajax( {
type: "POST",
url: "foo.php",
dataType: "json",
success: function( data ){
response( data );
}
}));
}
});