-
-
Save virtix/277306 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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