Skip to content

Instantly share code, notes, and snippets.

@heycarsten
Created August 29, 2012 03:21
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save heycarsten/3506485 to your computer and use it in GitHub Desktop.
Save heycarsten/3506485 to your computer and use it in GitHub Desktop.
Underscore's debounce and limit as Function prototypes.
// These are yoinked from Underscore.js but are added to the function prototype
// so that we can ruin the world.
(function() {
var debounce, throttle;
debounce = function(func, wait, immediate) {
var timeout;
return function() {
var context = this, args = arguments;
var later = function() {
timeout = null;
if (!immediate) func.apply(context, args);
};
if (immediate && !timeout) func.apply(context, args);
clearTimeout(timeout);
timeout = setTimeout(later, wait);
};
};
throttle = function(func, wait) {
var context, args, timeout, throttling, more, result;
var whenDone = debounce(function(){ more = throttling = false; }, wait);
return function() {
context = this; args = arguments;
var later = function() {
timeout = null;
if (more) func.apply(context, args);
whenDone();
};
if (!timeout) timeout = setTimeout(later, wait);
if (throttling) {
more = true;
} else {
result = func.apply(context, args);
}
whenDone();
throttling = true;
return result;
};
};
Function.prototype.debounce = function(wait, immediate) {
return debounce(this, wait, immediate);
};
Function.prototype.throttle = function(wait) {
return throttle(this, wait);
};
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment