Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
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