Created
April 24, 2012 22:03
-
-
Save pedroteixeira/2484196 to your computer and use it in GitHub Desktop.
Crates buffered function
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
// Calls a function only once within the given wait time. Last call is | |
// considered, which is the difference between from _.throtle | |
// | |
_.buffer = function(func, wait, scope) { | |
var timer = null; | |
return function() { | |
if(timer) clearTimeout(timer); | |
var args = arguments; | |
timer = setTimeout(function() { | |
timer = null; | |
func.apply(scope, args); | |
}, wait); | |
}; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Note, you can use _.debounce for this now.