Throttle & Debounce Utils

by Antawn Jamison March 12, 2026 Public
72 views Raw Download Revisions (v1)

Revision History

No revision history recorded yet.

throttle.js javascript Raw
/**
 * Throttle: fire at most once per `limit` ms
 */
export function throttle(fn, limit = 200) {
  let inThrottle = false;
  return function (...args) {
    if (!inThrottle) {
      fn.apply(this, args);
      inThrottle = true;
      setTimeout(() => (inThrottle = false), limit);
    }
  };
}

/**
 * Debounce: fire only after `delay` ms of silence
 */
export function debounce(fn, delay = 300) {
  let timer;
  return function (...args) {
    clearTimeout(timer);
    timer = setTimeout(() => fn.apply(this, args), delay);
  };
}
Skip to toolbar