Last active
December 18, 2015 00:38
-
-
Save jamsesso/5697755 to your computer and use it in GitHub Desktop.
jQuery plugin to detect when a user has finished typing in a field. Fires a callback function when typing is complete & has configurable timeout.
This file contains hidden or 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
/** | |
* jQuery typingFinished plugin. Fire a callback function when the user is finished typing. | |
* @param timeout The timeout in milliseconds per interval. | |
* @param callback The callback function to fire when the user finishes typing. | |
* @return Object | |
* | |
* Example: | |
* $('input').typingFinished(500, function(e) { | |
* console.log('Done typing. Last key pressed: '+ e.keyCode); | |
* }); | |
*/ | |
$.fn.typingFinished = function(timeout, callback) { | |
var timer = undefined, | |
self = $(this); | |
self.keyup(function(e) { | |
if(typeof timer == "undefined") { | |
timer = setTimeout(function() { | |
callback(e); | |
}, timeout); | |
} | |
}).keydown(function(e) { | |
if(typeof timer !== "undefined") { | |
clearTimeout(timer); | |
timer = undefined; | |
} | |
}); | |
return self; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment