Skip to content

Instantly share code, notes, and snippets.

@coreyward
Created July 29, 2011 23:33
Show Gist options
  • Select an option

  • Save coreyward/1114972 to your computer and use it in GitHub Desktop.

Select an option

Save coreyward/1114972 to your computer and use it in GitHub Desktop.
Debounce in CoffeeScript
# Debounce and SmartResize (in jQuery) ported to CoffeeScript
# debouncing function from John Hann
# http://unscriptable.com/index.php/2009/03/20/debouncing-javascript-methods/
debounce = (func, threshold, execAsap) ->
timeout = false
return debounced = ->
obj = this
args = arguments
delayed = ->
func.apply(obj, args) unless execAsap
timeout = null
if timeout
clearTimeout(timeout)
else if (execAsap)
func.apply(obj, args)
timeout = setTimeout delayed, threshold || 100
# Smartresize
jQuery.fn['smartresize'] = (fn) ->
if fn then this.bind('resize', debounce(fn)) else this.trigger('smartresize')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment