Throttle Polyfill

UtilitiesPolyfill

Implement a throttle(fn, limit) function.

Function Signature:

function throttle(fn, limit) { }

Requirements:

  • Return a new function that executes fn at most once per limit ms
  • Leading-edge: fire immediately on the first call, then block for limit ms
  • Preserve this context and all arguments

Bonus:

  • Implement the trailing-call variant using timestamps so the final blocked call also executes

Examples

Example 1
Input
const throttled = throttle(() => console.log('tick'), 100);
throttled(); throttled(); throttled();
// all three fired within 50ms
Output
tick
Explanation
Only the first call executes immediately. The next two are blocked within the 100ms window.

Notes

  • Debounce waits for silence. Throttle fires on a fixed cadence. Be ready to state that difference in one sentence.

Hints

Read the full write-up for Throttle Polyfill
</>JavaScript
Loading editor…
Test Result
Run your code, or Submit to test it