[英]how to prevent to send ajax request while another ajax request is processing?
I work on an auto complete search box that search Database and return users that related to search.I use jquery ajax to do this.when keyup
on input element occurred then I send a ajax request to the `/ajax/search/ url like below: 我上的自动完成搜索框,搜索数据库的工作并返回相关search.I使用jQuery AJAX用户做this.when
keyup
上出现输入元素,然后我送一个Ajax请求`/ AJAX /搜索/网址,如下所示:
$("#search").keyup(function(){
$(".itemSearch").remove();
$(".notfound").hide();
$("#searchResultSection").height("70px");
if(!$("#search").val()){
$("#searchResultSection").slideUp();
$(".ajaxLogoSearch").hide();
$(".notfound").hide();
}
else{
var data = {
query: $("#search").val()
};
$("#searchResultSection").slideDown();
$(".ajaxLogoSearch").show();
$.ajax({
url: '/ajax/search',
data: data,
dataType:'json',
success:function(result){
if(result.found==0){
$(".ajaxLogoSearch").hide();
$(".notfound").show();
}
else{
var searchResult = $("#searchResultSection");
$(".ajaxLogoSearch").hide();
searchResult.css({"height":20});
for(var i=0;i<result.users.length;i++){
var content = '<div class="itemSearch">\
<img src="../../static/social/images/defaultMaleImage.png" height="50px">\
<p>'+ result.users[i].firstname+ ' '+ result.users[i].lastname +'</p>\
</div>'
if(searchResult.height() < 370){
searchResult.height("+=70px");
}
$("#innerSearch").append(content);
}
$(".itemSearch").hover(function(){
$(this).css({"background":"rgb(55, 108, 221)"});
},function(){
$(this).css({"background":"#658be6"});
});
}
}
});
}
})
My problem is when I type 2 letters immediately 2 ajax request sent and I see the search result twice in search result box.I want a way to prevent to send 2th ajax request while first is not completely done.How can I do this ? 我的问题是当我立即键入2个字母2 ajax请求发送,我在搜索结果框中看到搜索结果两次。我想要一种方法来防止发送第2个ajax请求,而第一个没有完全完成。我怎么能这样做?
EDIT:I finally find the solution in codeproject site thanks for your answers. 编辑:我终于在codeproject网站找到解决方案感谢您的答案。 this is thte link of the solution
这是解决方案的链接
The better way is to simply not send a request until the user stops typing. 更好的方法是在用户停止输入之前不发送请求。 this is easily done using the keydown event and a throttle waiting for 250ms of inactivity.
使用keydown事件和等待250毫秒不活动的油门很容易完成。 Doing it your way will result in not getting the results that the user wants.
按照自己的方式进行操作将导致无法获得用户想要的结果。
$("#search").on("keydown",function(){
var $this = $(this);
clearTimeout($this.data("throttle"));
$this.data("throttle",setTimeout(function(){
doSearch.call($this.get(0),$this.val()); // perform the ajax search
},250))
});
Also, when inserting the new results, build the entire result list in a single variable then insert it all at once with .html(thehtml) after the loop. 此外,在插入新结果时,在单个变量中构建整个结果列表,然后在循环后使用.html(thehtml)将其全部插入。
You should create an additional variable kind of request. 您应该创建一个额外的变量类型的请求。 This variable will be store link of current ajax-request.
该变量将是当前ajax-request的存储链接。 When keyup event fire again you should do checking.
当keyup事件再次触发时,您应该进行检查。
if(request) {
request.abort();
}
request = $.ajax({/* */});
UPD1: If first ajax-request completed you should set to zero request variable in success callback. UPD1:如果第一个ajax-request完成,则应在成功回调中将请求变量设置为零。
UPD2: Subscribe on change event is better UPD2:订阅变更事件更好
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.