Javascript Instant Search Feature

I use the following javascript for my instant search function (to detect when the visitor stops writing, so the function will not start on every single key).

It works, but its more than 1000 milliseconds. Even if I set it to 200 milliseconds for a delay of 1-2 seconds before starting the instant search function.

Is there a better / faster way to detect when a visitor has stopped entering input (I only need this for Internet Explorer, if that matters).

$(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 cannot continue writing to the input file when the function starts. Any solution for this and I will not need any delay function.

+2
3
function instantSearch(){ ... }

var timer;
$('input').keyup(function(){
   timer && clearTimeout(timer);
   timer = setTimeout(instantSearch, 200);
});
+11

, AJAX ? 200 1-2 .

0

Source: https://habr.com/ru/post/1614266/


All Articles