Created
January 10, 2014 18:51
-
-
Save positlabs/8360280 to your computer and use it in GitHub Desktop.
limit the rate of firing for a function
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
function rateLimit(func, time){ | |
var callback = func, | |
waitTimeout, | |
waiting = false, | |
context = this; | |
var rtn = function(){ | |
if(waiting) return; | |
waiting = true; | |
var args = arguments; | |
waitTimeout = setTimeout(function(){ | |
waiting = false; | |
callback.apply(context, args); | |
}, time); | |
}; | |
return rtn; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment