Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Instant search function in 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).

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.

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.

like image 831
Hakan Avatar asked Jul 28 '26 22:07

Hakan


1 Answers

function instantSearch(){ ... }

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

Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!