Skip to content

debounce

Creates a debounced version of a function that delays invocation until after a specified number of milliseconds have elapsed since the last call.

import { debounce } from "1o1-utils";
import { debounce } from "1o1-utils/debounce";
function debounce<T>({ fn, ms }: DebounceParams<T>): Debounced<T>
NameTypeRequiredDescription
fnTYesThe function to debounce
msnumberYesDelay in milliseconds (non-negative)

Debounced<T> — The debounced function with a .cancel() method.

const debouncedSearch = debounce({
fn: (query: string) => search(query),
ms: 300,
});
input.addEventListener("input", (e) => {
debouncedSearch(e.target.value);
});
// Cancel pending execution
debouncedSearch.cancel();
  • Throws if fn is not a function.
  • Throws if ms is not a number or is NaN.
  • Throws if ms is negative.
  • Calling .cancel() clears any pending timeout.
  • Each call resets the delay timer.

delay call, wait idle, input delay, search delay

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 debounce to delay a search input API call