Skip to content

Instantly share code, notes, and snippets.

@Haprog
Created February 22, 2016 12:33
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save Haprog/d42832caf052fc9ab9e6 to your computer and use it in GitHub Desktop.
Rate Limiting JavaScript Function Call
// Returns a new function that will call the given "func" at most once every "wait" milliseconds.
// Based on debounce function from: https://davidwalsh.name/essential-javascript-functions
function rateLimit(func, wait) {
var waiting = false;
return function() {
if (!waiting) {
var waiting = true, context = this, args = arguments;
func.apply(context, args);
setTimeout(function(){ waiting = false; });
}
};
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment