Skip to content

Instantly share code, notes, and snippets.

@apinstein
Created October 25, 2010 19:59
Show Gist options
  • Save apinstein/645619 to your computer and use it in GitHub Desktop.
Save apinstein/645619 to your computer and use it in GitHub Desktop.
Simple function wrapper that can be applied to any function to allow it to be called frequently, but only run when it has not been called within a certain amount of time. Great way to throttle specific types of requests.
// Magic-ness to only run the save callback after no saves have been issued for a while
var executeAfter = function(f, ms)
{
var timer;
var wrapper = function()
{
var passedArguments = arguments;
if (timer)
{
window.clearTimeout(timer);
timer = null;
}
timer = window.setTimeout(function() {
f.apply(window, passedArguments);
}, ms);
}
return wrapper;
};
saveCallback = executeAfter(saveCallback, 500);
// now you can call "saveCallback" with impunity!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment