Skip to content

Instantly share code, notes, and snippets.

@akre54
Created April 3, 2015 20:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save akre54/e7528189db994ee49e58 to your computer and use it in GitHub Desktop.
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`.
# 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