1. Cooldown flag
A boolean tracks whether we are inside the cooldown window.
let inThrottle = false;Typorithm · medium
Step 1 of 8 · intro card
Utility Functions
throttle lets fn run at most once per limit milliseconds. The first call fires immediately, then a cooldown flag drops further calls until the interval elapses.
throttle returns a wrapper that enforces a steady maximum rate: fn runs at most once every limit milliseconds no matter how often it is called. It suits scroll, mousemove, and other high-frequency events.
A boolean flag gates the call. The first call passes the gate, runs fn immediately, and sets inThrottle to true; calls during the cooldown are simply ignored.
Contrast with debounce: debounce waits for activity to stop and fires once at the end, while throttle fires regularly during continuous activity at a fixed cadence.
Runs fn at most once per interval.
First call fires immediately (leading edge).
Great for scroll and mousemove handlers.
function throttle(fn, limit) {
let inThrottle = false;
return (...args) => {
if (!inThrottle) {
fn(...args);
inThrottle = true;
setTimeout(() => inThrottle = false, limit);
}
};
}Connect each part of the code to the concept before you type.
A boolean tracks whether we are inside the cooldown window.
let inThrottle = false;fn runs only when not currently throttled; otherwise the call is dropped.
if (!inThrottle) {Throttle fires fn right away on the leading edge, then blocks for the interval.
fn(...args);After limit milliseconds the flag resets, allowing the next call through.
setTimeout(() => inThrottle = false, limit);Read the concept map and follow the code breakdown.
Type the implementation, repair mistakes and work through every recall exercise.
Use the final check without hints; revisit weak concepts in short practice sessions.