Skip to content

Latest commit

 

History

History
26 lines (18 loc) · 791 Bytes

throttle.md

File metadata and controls

26 lines (18 loc) · 791 Bytes

throttle

Calls a function at most once per wait ms.

import { throttle } from 'mxs';

window.addEventListener('scroll', throttle(updatePosition, { wait: 100 }));

Signature

throttle<Args>(fn: (...args: Args) => void, options: { wait: number; signal?: AbortSignal }): Throttled<Args>
Option Description
wait Minimum time in ms between calls.
signal Aborting cancels the pending call and ignores later ones.

Behavior

  • The first call runs immediately.
  • Calls during the window are collapsed. The last one runs when the window ends.
  • cancel drops the pending call. flush runs it now.