English 中文(简体)
Javascript jquery——我如何在搜索条目中制造延误
原标题:Javascript jquery - How do I build a delay into the search entry

I have an ajax search box that goes to the server on every key stroke and returns the result of the search. When the user types quickly, I want to search only on the last entry and not on every key stroke. Otherwise the individual results flash annoyingly and the whole process is slowed.

For example: if the user types "statue of liberty" quickly, I don t want to search on "sta", "stat", "statu" etc.

the basics of my jQuery code is:

$( #searchbox ).keyup(function(){
    if (this.value.length > 2) {    
        $.post("remote.php",{ partial :this.value},function(data){
            $("#gen_results").html(data);
        });
    }
});        


<input id="searchbox" />
<div id="gen_results"></div>
最佳回答

use setTimeout or jQuery s autocomplete plugin

var timer;
$( #searchbox ).keyup(function(){
    if (this.value.length > 2) {
        if (timer){
                clearTimeout(timer);
        }
        timer = setTimeout(function(){
                $.post("remote.php",{ partial :this.value},function(data){
                $("#gen_results").html(data);
                });
        }, 1000);
    }
});
问题回答

You need to use a timeout, this one is set to 500ms, but you might want to go quicker.

$( #searchbox ).keyup(function(){
  window.clearTimeout(window.timeOutId);
  window.timeOutId = window.setTimeout(function() {
    if (this.value.length > 2) {    
      $.post("remote.php",{ partial :this.value},function(data){
       $("#gen_results").html(data);
      });
    }
  },500); 
}); 

希望能为你们工作!

引言

var inProgress = false;
$( #searchbox ).keyup(function(){
    if (this.value.length > 2 && !inProgress) {    
        inProgress  = true;
        $.post("remote.php",{ partial :this.value},function(data){
            $("#gen_results").html(data);
            inProgress = false;
        });
    }
}); 

这样,如果先前的电话是完整的,到那时用户已经做了更多的打字,那么你就不得不维持一个时间。

function getResults(value) {
   return function() {
       $.post("remote.php",{ partial :value},function(data){
           $("#gen_results").html(data);
       });
   };
}

var timerId;
$( #searchbox ).keyup(function(){
    if (this.value.length > 2) {
          clearTimeout(timerId);
          timerId = setTimout(getResults(this.value), 1000);
    }
});  

我曾经做过类似的事情。 我每次500万米都设定了时间,当叫作时间者时,日本宇宙航空研究开发机构的请求就是这样。 骗局是,每当用户打什么时,我会重新打造时间。

你们可以拥有一个全球变量,掌握着最后一个重要的中风。 当关键方法被称作时,需要时间阅读,并与以前的数值相比较。 如果时间已经过一段时间,你就会发出呼吁,否则就会重新确定全球价值和撤出。





相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签