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
Section titled “Import”import { debounce } from "1o1-utils";import { debounce } from "1o1-utils/debounce";Signature
Section titled “Signature”function debounce<T>({ fn, ms }: DebounceParams<T>): Debounced<T>Parameters
Section titled “Parameters”| Name | Type | Required | Description |
|---|---|---|---|
| fn | T | Yes | The function to debounce |
| ms | number | Yes | Delay in milliseconds (non-negative) |
Returns
Section titled “Returns”Debounced<T> — The debounced function with a .cancel() method.
Examples
Section titled “Examples”const debouncedSearch = debounce({ fn: (query: string) => search(query), ms: 300,});
input.addEventListener("input", (e) => { debouncedSearch(e.target.value);});
// Cancel pending executiondebouncedSearch.cancel();Edge Cases
Section titled “Edge Cases”- Throws if
fnis not a function. - Throws if
msis not a number or isNaN. - Throws if
msis negative. - Calling
.cancel()clears any pending timeout. - Each call resets the delay timer.
Also known as
Section titled “Also known as”delay call, wait idle, input delay, search delay
Prompt suggestion
Section titled “Prompt suggestion”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