终止所有正在进行的XHR请求
kill all ongoing XHR requests
终止所有正在进行的XHR请求
$('#search-box').keyup(function(){//绑定搜索数据var input=$('.search input').val();
$.getJSON({ // get JSON data
url: 'example/query.php?keyword=' + input,
//pre-load
beforeSend: function() {
$(".search-lists").html("<span class='loading'><img src='_/images/loading.gif' /></span>");
},
success: function(data) {
if (input.length >= 3) { // limit keyword to >=3
var output = "<ul class='search-lists'>"; //output search data list
$.each(data, function(key, val) {
output += '<li>';
output += '<a>' + val.term + '</a>';
output += '</li>';
});
output += '</ul>';
$('.search-results').html(output);
console.log('load ajax');
} // end if
else {
console.log('kill ajax');
}
}
}); // JSON request
}); // data bind
您必须首先进行检查,而不是进行筛选。此外,我建议使用setTimeout
来减少服务器调用:
<section id="search-box">
<form class="search-field">
<input id="search" class="search-input" type="text" value="Hello, I'm looking for..." />
</form>
<div class="search-results"></div>
</section>
var timer;
$('#search-box').keyup(function() { // bind the search data
clearTimeout(timer);
var input = $('.search-input').val();
if (input.length < 3) {
return;
}
timer = setTimeout(function () {
$.getJSON({ // get JSON data
url: 'http://test.sonsuzdongu.com/query.php?keyword=' + input,
//pre-load
beforeSend: function() {
$(".search-lists").html("<span class='loading'><img src='_/images/loading.gif' /></span>");
},
success: function(data) {
var output = "<ul class='search-lists'>"; //output search data list
$.each(data, function(key, val) {
output += '<li><a>' + val.term + '</a></li>';
});
output += '</ul>';
$('.search-results').html(output);
console.log('load ajax');
}
}); // JSON request
}, 300); //setTimeout
}); // data bind
若要终止所有请求,您可以尝试重新加载页面(请求将终止于)。或者简单地添加一些标志,指示是否需要处理进一步的输出。
success: function (data) {
if (!data.isEmptyObject()) {
// do processing.
}
}
有一个abort
方法用于取消xhr请求。您可以根据自己的要求使用它。
只需像这样存储请求,然后您可以随时中止请求。
var request = null;
....
....
function fetchJSON (){
if(request != null) {
request.abort();
}
request = $.getJSON({ // get JSON data
url: 'example/query.php?keyword=' + input,
//pre-load
beforeSend: function() {
$(".search-lists").html("<span class='loading'><img src='_/images/loading.gif' /></span>");
},
success: function(data) {
request = null;
if (input.length >= 3) { // limit keyword to >=3
var output = "<ul class='search-lists'>"; //output search data list
$.each(data, function(key, val) {
output += '<li>';
output += '<a>' + val.term + '</a>';
output += '</li>';
});
output += '</ul>';
$('.search-results').html(output);
console.log('load ajax');
} // end if
else {
console.log('kill ajax');
}
}
}); // JSON request
}); // data bind
}
相关文章:
- 是否可以缓存可下载的文件,并在XHR和非XHR请求之间共享该缓存
- 通过XHR请求将MYSQL数组从PHP返回到Javascript
- 正在发送请求中包含数组的qx.io.request.Xhr请求
- XHR请求的CSRF预防
- onReadyStateChange未在XHR请求的IE中激发
- 终止所有正在进行的XHR请求
- Web 工作线程中的同步 XHR 请求是否仍会锁定浏览器
- 在使用量角器进行e2e测试时记录xhr请求
- Angular在不触发摘要的情况下执行XHR请求
- 如果我使用xhr请求,那么ajaxComplete的等价物是什么
- 总是在XHR请求中附加一些内容
- 同时发出的最大 xhr 请求量
- Dojo 两个 XHR 请求和回调函数仅在两者完成时触发
- 我可以/如何显式终止长时间运行的 xhr 请求
- 服务工作线程是否可以响应同步 XHR 请求
- 如何将 xhr 请求转换为角度$http请求
- 如何捕获任何 XHR 请求
- 如何显示 XHR 请求的加载图标
- 发送 XHR 请求而不接收任何数据
- 返回 WinJS.xhr 请求结果