Created
April 3, 2015 20:00
-
-
Save akre54/e7528189db994ee49e58 to your computer and use it in GitHub Desktop.
Get a notification when a user starts and stops typing on the passed-in `el`.
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
# Get a notification when a user starts and stops typing on the passed-in `el`. | |
# | |
# Based on https://github.com/narfdotpl/jquery-typing | |
module.exports = (el, {delay, start, stop}) -> | |
delay ||= 400 | |
typing = false | |
timeout = null | |
startTyping = -> | |
unless typing | |
typing = true | |
start?() | |
stopTyping = -> | |
if typing | |
clearTimeout timeout | |
timeout = setTimeout -> | |
typing = false | |
stop?() | |
, delay | |
BACKSPACE_KEY = 8 | |
DELETE_KEY = 46 | |
el.addEventListener 'keypress', startTyping | |
el.addEventListener 'keydown', (e) -> | |
startTyping() if e.keyCode is BACKSPACE_KEY || e.keyCode is DELETE_KEY | |
el.addEventListener 'keyup', stopTyping |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment