Throttle in JavaScript
Lets one call through per interval and drops the rest, which is what a scroll handler wants.
function throttle(fn, limit) {
let ready = true;
return function (...args) {
if (!ready) return;
ready = false;
fn.apply(this, args);
setTimeout(() => {
ready = true;
}, limit);
};
}
Keywords and builtins used here
functionifletreturnthis
The run, in numbers
- Lines
- 11
- Characters to type
- 182
- Tokens
- 58
- Three-star pace
- 105 tpm
At the three-star pace of 105 tokens a minute, this run takes about 33 seconds.
Step 5 of 5 in Composing functions, step 9 of 16 in Functions & patterns.