Skip to content

Instantly share code, notes, and snippets.

@xeoncross
Last active May 21, 2018 20:41
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 xeoncross/2112e604cbf728a4852ae1a0529f3d4f to your computer and use it in GitHub Desktop.
Save xeoncross/2112e604cbf728a4852ae1a0529f3d4f to your computer and use it in GitHub Desktop.
Simple rate-limit function for creating limit functions that can be called repeatedly. https://codepen.io/Xeoncross/pen/WJmrge
function rateLimit(rate, perMils) {
let x = 0;
return () => {
if (x < rate) {
++x;
setTimeout(function() {--x;}, perMils);
return false;
}
return true;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment