Skip to content

Instantly share code, notes, and snippets.

@positlabs
Created January 10, 2014 18:51
Show Gist options
  • Save positlabs/8360280 to your computer and use it in GitHub Desktop.
Save positlabs/8360280 to your computer and use it in GitHub Desktop.
limit the rate of firing for a function
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