Skip to content

Instantly share code, notes, and snippets.

@Tronix117
Last active August 29, 2015 13:56
Show Gist options
  • Save Tronix117/8816559 to your computer and use it in GitHub Desktop.
Save Tronix117/8816559 to your computer and use it in GitHub Desktop.
Prevent 'click' callback to be called when 'dblClick' callback should be called
# Saferize Click
#
# saferClick = saferizeClick singleClickCallback, doubleClickCallback
#
# Prevent 'click' callback to be called when 'dblClick' callback should be called
#
# Exemple:
#
# Better to add it to underscore with following:
#
# _.mixin require('lib/saferize-click')
#
# Then use it like following:
#
# saferClick = _.saferizeClick ((e)-> console.log '1 click'), ((e)-> console.log '2 clicks')
#
# $('something')
# .on 'click', saferClick.click
# .on 'dblclick', saferClick.dblClick
#
# @author Jeremy Trufier <[email protected]> (https://github.com/Tronix117)
# @gist https://gist.github.com/Tronix117/8816559
# @jsfiddle http://jsfiddle.net/y8Y69/3/
# @license MIT
# -------------------------------------
module.exports =
saferizeClick: (sglCb, dblCb, triggerDuration = 500)->
_timer = null
click: (e)->
args = arguments
self = @
clearTimeout _timer
_timer = setTimeout((-> sglCb.apply self, args), triggerDuration)
dblclick: (e)->
clearTimeout _timer
dblCb.apply @, arguments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment