Throttle repeated work using an explicit interval
Quick answer
Throttling limits how often a function can run while calls continue. A leading-only throttle runs the first eligible call immediately and ignores calls during its interval.
How it works
Store the timestamp of the last accepted call. Accept a new call when now - last >= interval; initialize last to negative infinity so the first call is accepted. This lesson assumes nondecreasing timestamps. A leading-only policy drops suppressed calls; a trailing policy must additionally arrange a later delivery of the latest suppressed arguments. For visual updates consider coalescing work into one animation frame. Use injected timestamps for deterministic tests of acceptance; this is not a guarantee of real timer precision.
Local examples
Run in a fresh browser document with local synthetic data. Actual ByteMind editor support is unverified.
(async () => {let last=-Infinity;const accepted=[];for(const t of [0,20,100,150,200]){if(t-last>=100){accepted.push(t);last=t;}}console.log(JSON.stringify(accepted));})()Expected console output:
[0,100,200]Rejected calls do not reset the interval.
Run in a fresh browser document with local synthetic data. Actual ByteMind editor support is unverified.
(async () => {let pending=null,latest;const rendered=[];function update(x){latest=x;if(pending===null)pending=requestAnimationFrame(()=>{pending=null;rendered.push(latest);});}update(1);update(2);update(3);await new Promise(resolve=>requestAnimationFrame(resolve));console.log(JSON.stringify(rendered));})()Expected console output:
[3]The three synchronous updates are coalesced into one frame callback with the latest value.
Common mistakes
Do not update the last accepted timestamp for a rejected call: that can accidentally implement a quiet-period policy. Do not promise the last input is delivered by a leading-only throttle.
Practice
Compare a 100 ms leading-only throttle against a trailing debounce for calls at 0, 20, 100 and 150 ms. Explain which events are dropped.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Throttle repeated work using an explicit interval — Knowledge Check
Apply the concept and reason about local example output.
Which timestamp should a leading-only throttle remember?
Does a leading-only throttle guarantee a final trailing call?
What is a suitable coalescing strategy for many synchronous visual updates?
What is the exact console output in Throttle repeated work using an explicit interval example 1?
(async () => {let last=-Infinity;const accepted=[];for(const t of [0,20,100,150,200]){if(t-last>=100){accepted.push(t);last=t;}}console.log(JSON.stringify(accepted));})()What is the exact console output in Throttle repeated work using an explicit interval example 2?
(async () => {let pending=null,latest;const rendered=[];function update(x){latest=x;if(pending===null)pending=requestAnimationFrame(()=>{pending=null;rendered.push(latest);});}update(1);update(2);update(3);await new Promise(resolve=>requestAnimationFrame(resolve));console.log(JSON.stringify(rendered));})()