Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
JS Rate Limiting
// Returns a function that won't fire more than
// once every `wait` milliseconds.
//
// Example usage:
//
// $('.button').click(rateLimit(function(){
// console.log('clicked!');
// }));
//
function rateLimit(func, wait) {
var timeout;
return function() {
var context = this, args = arguments;
if (!timeout) {
func.apply(context, args);
}
timeout = setTimeout(function() {
timeout = null;
}, wait);
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.