admin管理员组

文章数量:1287931

I have an input on an HTML page where a person will type something and what I'm wanting is for a function to be called every time they stop typing. I know about the onkeypress event, but that is called every time a character is typed. Here's an example of what I would like:

  1. Person types "this is a test" into the input
  2. After the person stops typing, function foo() should be called
  3. Person types something else into the input
  4. After the person stops typing, function foo() should be called
  5. Repeat....

Is this possible?

Thanks a lot!

I have an input on an HTML page where a person will type something and what I'm wanting is for a function to be called every time they stop typing. I know about the onkeypress event, but that is called every time a character is typed. Here's an example of what I would like:

  1. Person types "this is a test" into the input
  2. After the person stops typing, function foo() should be called
  3. Person types something else into the input
  4. After the person stops typing, function foo() should be called
  5. Repeat....

Is this possible?

Thanks a lot!

Share Improve this question asked Jun 23, 2012 at 20:40 NateNate 28.4k38 gold badges136 silver badges228 bronze badges 9
  • 7 Define "person stopped typing"... – Oded Commented Jun 23, 2012 at 20:42
  • 2 you can handle mouseup of loss focus event. But not person stop typing – Umesh Aawte Commented Jun 23, 2012 at 20:44
  • @Oded - As in they type "this is a test," then stop pressing keys on the keyboard. So there should be maybe a second delay. – Nate Commented Jun 23, 2012 at 20:44
  • One second? And if they are thinking? Or gone to another room to lookup stuff? – Oded Commented Jun 23, 2012 at 20:45
  • 2 stackoverflow./questions/2219924/… – Rajat Singhal Commented Jun 23, 2012 at 20:45
 |  Show 4 more ments

4 Answers 4

Reset to default 11

Listen to onkeyupevent. There start a timeout (you have to define "stop" as some time amount, otherwise it will be called at every keyup), and in the timeout execute your function.

If onkeydown is called while your timer is running, stop it.

Something like...

var timer;

function onKeyUpHandler(e){
    timer = setTimeout("foo()", 500)    
}

function onKeyDownHandler(e) {
    clearTimeout(timer);
}

How this code exactly looks depends where and how you are using it... just to show the way to do it.

Just set a javascript timer to start/restart on onKeyPress. Then just define how long defines "stops typing" and run your script.

First, capture the "key up" event, and set a "stoppedTyping" variable to true. Then set a timer (how long you want to wait before considering this a "stopped typing" event).

If, before the timer goes off, you capture a "key down" event, set "stoppedTyping=false"

When the timer goes expires, your timer callback can check the value of "stoppedTyping". If it's true, then the user has stopped typing (or more precisely, has not typed any new chars for the duration of your timer). If it's false, then they have not stopped typing -- they must have typed at least one more char while your timer was running.

If I understand well, "stop typing" means "no keystrokes for a certain amount of time". In this case, you have to use timers : http://jsfiddle/xnghR/1/

本文标签: javascriptHow can I call a function after a person stops typingStack Overflow