Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Run functions when user stops typing

Status
Not open for further replies.

jdbolt

Programmer
Aug 10, 2005
89
CA
I have been looking into how I can figure whether a user has stopped typing in a text box, but so far I havent found a decent solution.

Basically I need a way to run a function after I am sure the user has stopped typing in the text box (but while the text box is still in focus)
 
then you'll need to define what you mean by "when the user stops typing". in other words, this can be done by restarting a counter on each keystroke. if the counter exceeds your limit, then something can happen. just need to know your definition of "stopped typing".

*cLFlaVA
----------------------------
[tt]I already made like infinity of those at scout camp...[/tt]
[URL unfurl="true"]http://www.coryarthus.com/[/url]
[banghead]
 
> Use a setTimeout and reset it using the keyDown event on the text field.

How would I do this?
 
Code:
<SCRIPT LANGUAGE="JavaScript">
var timeoutID = '';
function restartTimer() {
    window.clearTimeout(timeoutID);
    startTimer();
}

function startTimer() {
    window.clearTimeout(timeoutID);
    timeoutID = window.setTimeout("alert('you have stopped typing')",1000);
}
function keyDown()
   {
   startTimer();
   }
function stopTimer()
	{
	window.clearTimeout(timeoutID);
	}
document.onkeydown = keyDown;

</SCRIPT>

</HEAD>

<BODY BGCOLOR="#FFFFFF">
timer
<FORM NAME="frm">
<input type="text" name="data" value="" size=100 onFocus='javascript:startTimer();' onChange="javascript:stopTimer();"> type here


<BR>
<input type="text" name="data2" value="" size=10>
</FORM>
seconds...
</BODY>
</HTML>

I made this example. You may be able to make a better version, I am still new to javascript. But you can get an idea of how to work with the timers and keyDown events.

I don't know the answer but my good friend Google does.
 
Alternatively, you could just start the timer after the person types the first character and then, when the time is up, disable the textbox... maybe even hide it.

Then you will KNOW they have stopped typing!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top