OnKeyUp JavaScript Time Delay?

Spoonk picture Spoonk · Sep 5, 2009 · Viewed 25.5k times · Source

Hi Again Masters Of The Web :) Now, I have got a new stupid question, and I am asking to forgive me. I read everywhere about this solution, but didn't find the one that works for me.

I have got:

<input name="domain" type="text" id="domain" onKeyUp="javascript:chk_me();">

All I am asking is how to make this not to check after a button is pressed, but after to say 1000 miliseconds of keyboard inactivity?

Answer

mck89 picture mck89 · Sep 5, 2009

Try this:

var timer;
function chk_me(){
   clearTimeout(timer);
   timer=setTimeout(function validate(){...},1000);
}

In this way every time a key is pressed, the timeout will be deleted and the set again.