Throttling is a technique to limit how often a function executes.
When a function is throttled with a wait time of X ms:
X ms are ignored.X ms have passed since the last call, the function can run again.Your task: Implement throttle(fn, wait) which:
fn and a wait duration wait in ms.fn that respects the rules above.Example
let i = 0; function increment() { i++; } const throttledIncrement = throttle(increment, 100); // t = 0 → call runs immediately, i = 1 throttledIncrement(); // t = 50 → ignored (less than 100ms since last run), i = 1 throttledIncrement(); // t = 101 → allowed (more than 100ms since last run), i = 2 throttledIncrement();
Follow-up:
Enhance throttle to support:
cancel() method (clear pending executions).leading and trailing options (decide whether the function runs immediately, at the end, or both).JavaScript Function
No test results yet
Click "Run" to execute tests