Created
April 3, 2021 00:06
-
-
Save InfamousStarFox/cf000fc4221b91cfe10881ee5a308dcf to your computer and use it in GitHub Desktop.
Typescript Throttle
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
// 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