Skip to content

Instantly share code, notes, and snippets.

@jamsesso
Last active December 18, 2015 00:38
Show Gist options
  • Save jamsesso/5697755 to your computer and use it in GitHub Desktop.
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.
/**
* 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