Last active
April 22, 2016 23:49
-
-
Save dbiagi/b2f5c71721e0c2d9938bfde9c93f0a67 to your computer and use it in GitHub Desktop.
Javascript finish typing event.
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
| (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