Rate Limiting JavaScript Function Call
This file contains 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 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