Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OnKeyUp JavaScript Time Delay?

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?

like image 339
Spoonk Avatar asked Sep 04 '09 22:09

Spoonk


People also ask

How do I delay a Keyup event?

In this article, we will see how to use keyup with a delay in jQuery. There are two ways to achieve the same: Approach 1: Using the keypress(), fadeIn(), delay() and fadeOut() methods in the jQuery library and clearTimeout() and setTimeout() methods in native JavaScript.

How do you delay the Keyup handler until the user stops typing?

Use a variable to store the timeout function. Then use clearTimeout() to clear this variable of any active timeout functions, and then use setTimeout() to set the active timeout function again.

How do I clear set timeout?

To cancel a setTimeout() method from running, you need to use the clearTimeout() method, passing the ID value returned when you call the setTimeout() method.


1 Answers

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.

like image 142
mck89 Avatar answered Sep 20 '22 08:09

mck89