Created
February 11, 2012 03:31
-
-
Save hitode909/1795807 to your computer and use it in GitHub Desktop.
jQuery Deferred, throttle, debounce
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
debounce = (delay) -> | |
lastd = $.Deferred() | |
-> | |
lastd.reject() | |
d = $.Deferred() | |
lastd = d | |
args = arguments | |
setTimeout -> | |
d.resolve args | |
, delay || 100 | |
d.promise() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
th = throttle(500) | |
th('hello').then (msg) -> | |
console.log(msg) | |
de = debounce(1000) | |
de('hello').then (msg) -> | |
console.log(msg) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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