Created
October 25, 2010 19:59
-
-
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.
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
// 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