Created
July 8, 2015 15:52
-
-
Save colinodell/bbdf5a30d3ed24ce1ece to your computer and use it in GitHub Desktop.
JS Rate Limiting
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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