Skip to content

Instantly share code, notes, and snippets.

@hitode909
Created February 11, 2012 03:31
Show Gist options
  • Save hitode909/1795807 to your computer and use it in GitHub Desktop.
Save hitode909/1795807 to your computer and use it in GitHub Desktop.
jQuery Deferred, throttle, debounce
debounce = (delay) ->
lastd = $.Deferred()
->
lastd.reject()
d = $.Deferred()
lastd = d
args = arguments
setTimeout ->
d.resolve args
, delay || 100
d.promise()
th = throttle(500)
th('hello').then (msg) ->
console.log(msg)
de = debounce(1000)
de('hello').then (msg) ->
console.log(msg)
throttle = (delay) ->
d = null
->
return $.Deferred().promise() if d
d = $.Deferred()
args = arguments
setTimeout ->
d.resolve args
d = null
, delay || 100
d.promise()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment