TypeScript

Throttle (leading-edge, typed)

admin by @admin ADMIN
1d ago
May 31, 2026
Public
0 0 up · 0 down Sign in to vote
Pair to debounce: fire on the first call, then ignore further calls for `interval` ms. Useful for rate-limiting scroll handlers or button clicks.
TypeScript
Raw
export function throttle<Args extends unknown[]>(
  fn: (...args: Args) => void,
  interval: number,
): (...args: Args) => void {
  let nextAllowed = 0;
  return (...args) => {
    const now = Date.now();
    if (now >= nextAllowed) {
      nextAllowed = now + interval;
      fn(...args);
    }
  };
}

const onScroll = throttle(() => {
  console.log('scroll y =', window.scrollY);
}, 100);

window.addEventListener('scroll', onScroll);   // fires at most every 100ms
Tags

Save your own code snippets

Create a free account and build your private vault. Share publicly whenever you want.