Loading problem…
Implement a basic throttle function that limits how often a function can be called.
In this version, the throttled function should execute immediately on the first call, then ignore all subsequent calls made within the next delay milliseconds.
Unlike debounce, throttle does not wait for calls to stop. It allows one call, then blocks repeated calls until the delay window has passed.
fn and a delay delay in millisecondsfn immediately on the first calldelay millisecondsthis context of the call sitefnnull, undefined, NaN, or negative delay values as 00, calls are only blocked for the current synchronous execution timing and future calls may run again immediately on later ticksconst fn = (x) => console.log(x);
const throttled = throttle(fn, 100);
throttled(1); // runs immediately
throttled(2); // ignored
throttled(3); // ignored
setTimeout(() => {
throttled(4); // runs after 100ms window has passed
}, 120);