Skip to Content
ohooks

useThrottleCallback

Creates a throttled version of a callback that executes at most once every wait milliseconds.

Loading preview...

The useThrottleCallback hook returns a stable, throttled version of a callback that executes at most once per wait milliseconds, supporting leading and trailing execution edges as well as .cancel(), .flush(), and .isPending() controls.

Installation

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

Usage

Examples

Throttling Window or Container Events

Limit how frequently an expensive handler runs during continuous scroll or resize events.

Flushing or Cancelling a Trailing Call

Use .flush() to immediately run a pending trailing invocation or .cancel() to discard it.

API Reference

ThrottleOptions

Configuration options for leading and trailing execution edges.

Property
Specification
leading?
uniondefault:true
Invoke the function on the leading edge of the wait interval
trailing?
uniondefault:true
Invoke the function on the trailing edge of the wait interval

ThrottledFunction

Throttled function returned by the hook, augmented with .cancel(), .flush(), and .isPending().

Property
Specification
cancelrequired
function
Cancels any pending trailing invocation and resets the timer.
flushrequired
function
Immediately executes any pending trailing invocation.
isPendingrequired
function
Returns true if a trailing invocation timer is currently active.