Skip to Content
ohooks

useDebounceCallback

Creates a debounced version of a callback function with leading, trailing, maxWait, cancel, and flush support.

Loading preview...

The useDebounceCallback hook returns a stable, debounced version of a callback that delays invocation until delay milliseconds have elapsed since the last call, and exposes .cancel(), .flush(), and .isPending() control methods.

Installation

pnpm dlx shadcn@latest add https://ohooks.vercel.app/r/useDebounceCallback.json

Usage

Examples

Leading Edge and maxWait

Configure leading: true to invoke immediately on the first call, or set maxWait to guarantee execution at regular intervals during continuous input.

Flushing and Cancelling Pending Calls

Call .flush() to immediately execute a pending invocation (for example, on form submit or blur) or .cancel() to discard it.

API Reference

DebounceOptions

Configuration options for controlling debounce timing and execution edges.

Property
Specification
leading?
uniondefault:false
Invoke the callback on the leading edge of the timeout
trailing?
uniondefault:true
Invoke the callback on the trailing edge of the timeout
maxWait?
number
Maximum time in milliseconds the callback is allowed to be delayed before it is invoked

DebouncedFunction

Debounced function returned by the hook, augmented with control methods.

Property
Specification
cancelrequired
function
Cancels any pending delayed invocation
flushrequired
function
Immediately invokes any pending delayed invocation and returns the result
isPendingrequired
function
Returns true if there is a pending delayed invocation