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:
- Person types "this is a test" into the input
- After the person stops typing, function foo() should be called
- Person types something else into the input
- After the person stops typing, function foo() should be called
- 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:
- Person types "this is a test" into the input
- After the person stops typing, function foo() should be called
- Person types something else into the input
- After the person stops typing, function foo() should be called
- 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
4 Answers
Reset to default 11Listen 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
版权声明:本文标题:javascript - How can I call a function after a person stops typing? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741324828a2372413.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论