发出新呼叫时中止先前的AJAX呼叫? [英] Abort previous AJAX call when a new one made?
本文介绍了发出新呼叫时中止先前的AJAX呼叫?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
当用户键入搜索词时,我正在更新搜索结果.
I'm updating search results as the user types the search term.
当2个Ajax调用发生时,有时最后一个 data_response
首先被带回.我需要确保不会发生这种情况.
When 2 ajax calls happen, sometimes the last one data_response
is brought back first. I need to make sure this doesn't happen.
这是我的代码:
function filterCities(search) {
$.ajax({type:'GET',url:'/ventas/theme/citiesContainer.php',data: "search=" + search,
success:function(data_response){
results.innerHTML = data_response;
}});
}
创建新请求时,如何取消同一请求的先前实例?
How do I cancell previous instances of the same request when I make a new one?
推荐答案
这是一个带有简单计数器的解决方案:
This is a solution with a simple counter:
<script>
var counter = 0;
function filterCities(search,c) {
$.ajax({type:'GET',url:'/results.php',data: "search=" + search,
success:function(data_response){
if(c == counter) { // Only update if it's data_response from last call
results.innerHTML = data_response;
}
}});
}
</script>
<input type="text" onkeyup="counter++; filterCities(this.value,counter);">
这篇关于发出新呼叫时中止先前的AJAX呼叫?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文