Skip to content

Instantly share code, notes, and snippets.

@Haprog
Created February 22, 2016 12:33
Show Gist options
  • Save Haprog/d42832caf052fc9ab9e6 to your computer and use it in GitHub Desktop.
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