throttle

See debounce if you want your function to run at the end of the N seconds.

typescriptjavascript
const throttle = <T extends CallableFunction>(fn: T, delay: number): T => {
let canRun = true
return ((...args: any[]) => {
if (canRun) {
fn(...args)
canRun = false
setTimeout(() => {
canRun = true
}, delay)
}
}) as any
}
Created 03/28/21Updated 03/28/21
Found a mistake, or want to suggest an improvement? Edit on GitHub here
and see edit history here