Skip to content

Instantly share code, notes, and snippets.

@colinodell
Created July 8, 2015 15:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save colinodell/bbdf5a30d3ed24ce1ece to your computer and use it in GitHub Desktop.
Save colinodell/bbdf5a30d3ed24ce1ece to your computer and use it in GitHub Desktop.
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