Created
July 29, 2011 23:33
-
-
Save coreyward/1114972 to your computer and use it in GitHub Desktop.
Debounce in CoffeeScript
This file contains hidden or 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 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