Creates a debounced version of a function, which will only be invoked after a specified delay
has passed since the last time the debounced function was invoked. If immediate is true,
the function will be triggered at the start of the debounce delay, otherwise, it is triggered after.
This is useful for scenarios like limiting the rate of user input handling, resizing events,
or scroll events.
// In this example, `handleSearch` will only be called after 500ms since the last input // This prevents calling the function too frequently during rapid input changes.
Type Parameters
T
R
Parameters
wait: number
The number of milliseconds to wait before invoking the function after the last call.
constdebouncedLog = debounce(1000, (msg: string) =>console.log(msg), true); debouncedLog("Hello"); // Immediately logs "Hello", then waits for 1000ms for further calls
Creates a debounced version of a function, which will only be invoked after a specified delay has passed since the last time the debounced function was invoked. If
immediateis true, the function will be triggered at the start of the debounce delay, otherwise, it is triggered after.This is useful for scenarios like limiting the rate of user input handling, resizing events, or scroll events.
Example: