Skip to content

Instantly share code, notes, and snippets.

@dbiagi
Last active April 22, 2016 23:49
Show Gist options
  • Select an option

  • Save dbiagi/b2f5c71721e0c2d9938bfde9c93f0a67 to your computer and use it in GitHub Desktop.

Select an option

Save dbiagi/b2f5c71721e0c2d9938bfde9c93f0a67 to your computer and use it in GitHub Desktop.
Javascript finish typing event.
(function() {
var timerId = null,
waitingTime = 2000,
selector = 'input[type="text"],textarea',
isValid = function(keycode) {
return (keycode > 47 && keycode < 58) || // number keys
keycode == 32 || keycode == 13 || // spacebar & return key(s) (if you want to allow carriage returns)
(keycode > 64 && keycode < 91) || // letter keys
(keycode > 95 && keycode < 112) || // numpad keys
(keycode > 185 && keycode < 193) || // ;=,-./` (in order)
(keycode > 218 && keycode < 223) || // [\]' (in order)
(keycode == 46 || keycode == 8) //delete & backspace
}
$(document).on('keyup', selector, function(e) {
if(!isValid(e.keyCode)){
return
}
clearTimeout(timerId)
if (this.value) {
var callback = function(element) {
var evt = new CustomEvent('finishtyping');
element.dispatchEvent(evt)
}.bind(null, this)
timerId = setTimeout(callback, waitingTime)
}
})
})()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment