Skip to content

Latest commit

 

History

History
33 lines (26 loc) · 527 Bytes

File metadata and controls

33 lines (26 loc) · 527 Bytes

useThrottleFn

Purpose

Throttle a function call with run/cancel/flush/pending controls.

API

function useThrottleFn<T extends (...args: unknown[]) => void>(
  fn: T,
  wait: number,
  options?: {
    leading?: boolean;
    trailing?: boolean;
  }
): {
  run: (...args: Parameters<T>) => void;
  cancel: () => void;
  flush: () => void;
  pending: () => boolean;
};

Example

const throttled = useThrottleFn(() => {
  updateScroll();
}, 16);

window.addEventListener('scroll', throttled.run);