Skip to content

Instantly share code, notes, and snippets.

@virtix
Forked from codeodor/UserStoppedTyping.html
Created January 14, 2010 16:45
Show Gist options
  • Save virtix/277306 to your computer and use it in GitHub Desktop.
Save virtix/277306 to your computer and use it in GitHub Desktop.
<script type="text/javascript">
/**----------------------------------------------------------------
I reset timeOfLastKeyPress and the intervalId in the
userStoppedTyping so that it can continue to run
-----------------------------------------------------------------*/
var timeOfLastKeyPress = new Date().getTime() * 2; // make it in the future
var maxMillisecondsBetweenKeystrokes = 600;
var millisecondsBetweenCheckingForStoppage = 200;
var intervalId = setInterval("userStoppedTyping()", millisecondsBetweenCheckingForStoppage);
var logKeyPress = function (){
timeOfLastKeyPress = new Date().getTime();
}
var userStoppedTyping = function(){
if(new Date().getTime() - timeOfLastKeyPress > maxMillisecondsBetweenKeystrokes){
//this is where we do our expensive stuff.
alert("You stopped typing - " + document.getElementById('field').value);
clearInterval(intervalId);
timeOfLastKeyPress = new Date().getTime() * 2;
intervalId = setInterval("userStoppedTyping()", millisecondsBetweenCheckingForStoppage);
}
}
</script>
<input type="text" id="field" onkeypress="logKeyPress();">
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment