Typed throttle in TypeScript
Throttle with the wrapped function's signature preserved.
function throttle<A extends unknown[]>(
fn: (...args: A) => void,
limit: number,
): (...args: A) => void {
let ready = true;
return (...args: A) => {
if (!ready) return;
ready = false;
fn(...args);
setTimeout(() => {
ready = true;
}, limit);
};
}
Keywords and builtins used here
Aextendsfunctionifletnumberreturn
The run, in numbers
- Lines
- 14
- Characters to type
- 246
- Tokens
- 82
- Three-star pace
- 105 tpm
At the three-star pace of 105 tokens a minute, this run takes about 47 seconds.
Step 3 of 5 in Timing & memory, step 16 of 19 in Classes & patterns.