Skip to content

Instantly share code, notes, and snippets.

@InfamousStarFox
Created April 3, 2021 00:06
Show Gist options
  • Save InfamousStarFox/cf000fc4221b91cfe10881ee5a308dcf to your computer and use it in GitHub Desktop.
Save InfamousStarFox/cf000fc4221b91cfe10881ee5a308dcf to your computer and use it in GitHub Desktop.
Typescript Throttle
// Limit a function to run at max once per interval
// Example, throttle(callback, 100) called every 10ms. Callback will return once every 100ms.
// Example, throttle(callback, 100) called 30 times in 10ms, then not called again. Callback will return once.
export function throttle(callback:Function, time:number) {
let free:boolean = true;
return function(this:any){
if(free) {
callback.apply(this, arguments);
free = false;
setTimeout(() => {
free = true;
}, time);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment