Skip to content

throttle

Creates a throttled version of a function that only invokes the original at most once per specified interval.

import { throttle } from "1o1-utils";
import { throttle } from "1o1-utils/throttle";
function throttle<T>({ fn, ms }: ThrottleParams<T>): Throttled<T>
NameTypeRequiredDescription
fnTYesThe function to throttle
msnumberYesMinimum interval between calls in milliseconds (non-negative)

Throttled<T> — The throttled function with a .cancel() method.

const throttledScroll = throttle({
fn: () => updatePosition(),
ms: 100,
});
window.addEventListener("scroll", throttledScroll);
// Cancel pending trailing call
throttledScroll.cancel();
  • Throws if fn is not a function.
  • Throws if ms is not a number or is NaN.
  • Throws if ms is negative.
  • First call executes immediately.
  • Subsequent calls within ms are queued; the last one fires after the interval.
  • .cancel() clears pending calls and resets state.

rate limit, limit calls, scroll handler, resize handler

I'm using 1o1-utils (npm: https://www.npmjs.com/package/1o1-utils, GitHub: https://github.com/pedrotroccoli/1o1-utils, LLM context: https://pedrotroccoli.github.io/1o1-utils/llms.txt). Show me how to use throttle to limit a scroll event handler