Skip to main content
Back to problems
#2676
Medium JavaScript

Throttle

83.4% acceptance
Mar 31, 2026
171
30
Given a function fn and a time in milliseconds t, return a throttled version of that function. A throttled function is first called without delay and then, for a time interval of t milliseconds, can't be executed but should store the latest function arguments provided to call fn with them after the end of the delay. For instance, t = 50ms, and the function was called at 30ms, 40ms, and 60ms. At 30ms, without delay, the throttled function fn should be called with the arguments, and calling the throttled function fn should be blocked for the following t milliseconds. At 40ms, the function should just save arguments. At 60ms, arguments should overwrite currently stored arguments from the second call because the second and third calls are made before 80ms. Once the delay has passed, the throttled function fn should be called with the latest arguments provided during the delay period, and it should also create another delay period of 80ms + t. The above diagram shows how throttle will transform events. Each rectangle represents 100ms and the throttle time is 400ms. Each color represents a different set of inputs.

Solution

TypeScript
Time O(1)
Space O(1)
LeetCode
solution.ts
type F = (...args: number[]) => void

function throttle(fn: F, t: number): F {
  let lastArgs: number[] | null = null;
  let timer: ReturnType<typeof setTimeout> | null = null;
  
  return function (...args) {
    if (timer === null) {
      fn(...args);
      timer = setTimeout(() => {
        if (lastArgs !== null) {
          fn(...lastArgs);
          lastArgs = null;
          timer = setTimeout(function check() {
            if (lastArgs !== null) {
              fn(...lastArgs);
              lastArgs = null;
              timer = setTimeout(check, t);
            } else {
              timer = null;
            }
          }, t);
        } else {
          timer = null;
        }
      }, t);
    } else {
      lastArgs = args;
    }
  }
};

/**
 * const throttled = throttle(console.log, 100);
 * throttled("log"); // logged immediately.
 * throttled("log"); // logged at t=100ms.
 */