Javascript中的即时搜索功能 [英] Instant search function in Javascript
问题描述
我正在使用以下javascript作为我的即时搜索功能(以检测访问者何时停止写入,因此该函数不会在每个单独的键盘上运行)。
I am using the following javascript for my instant search function (to detect when the visitor stops writing, so the function won't run on every single keyup).
它可以工作,但它比1000毫秒更延迟。即使我将其设置为200毫秒,它也会在即时搜索功能运行之前延迟1-2秒。
It works but it’s more delay than 1000 milliseconds. Even if I set it to 200 milliseconds it’s 1-2 seconds delay before the instant search function runs.
是否有更好/更快的方式来检测访问者何时停止输入输入(我只需要它用于Internet Explorer,如果这有任何区别)。
Is there a better/faster way to detect when the visitor has stopped typing in the input (I only need it for Internet Explorer if that’s make any difference).
$(document).ready(function(){
var delay = (function(){
var timer = 0;
return function(callback, ms){
clearTimeout (timer);
timer = setTimeout(callback, ms);
};
})();
$('input').keyup(function() {
delay(function(){
//instant search function here
}, 1000 );
});
});
新想法:当我想到它的问题是我无法继续写入输入在函数运行时提交。任何解决方案,我将不需要任何延迟功能。
New idea: When I think about it the problem is that I can’t continue writing in the input filed when the function runs. Any solution for that and I will not be needing any delay function.
推荐答案
function instantSearch(){ ... }
var timer;
$('input').keyup(function(){
timer && clearTimeout(timer);
timer = setTimeout(instantSearch, 200);
});
这篇关于Javascript中的即时搜索功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!