Skip to content

@kaelyx/ts-kit / function / debounce

Function: debounce() ​

debounce<A>(fn, options?): DebounceHandler<A>

Defined in: function/debounce/debounce.ts:35

Delays a function until debounceMs milliseconds have passed since the last call. Only the latest call runs, with its arguments. Any return value from fn is ignored.

If fn throws, the error comes from the timer callback, or from flush when you call it. To get a result back from an async function, use debounceWithAbort.

Type Parameters ​

A ​

A extends unknown[]

Parameters ​

fn ​

(...args) => unknown

The function to delay.

options? ​

DebounceOptions

The delay. With no options, fn runs on the next timer tick after the last call.

Returns ​

DebounceHandler<A>

A handler with cancel and flush.

Example ​

ts
const save = debounce((value: string) => localStorage.setItem('draft', value), {
  debounceMs: 300,
})

save('a')
save('ab') // Only this call runs, 300ms after it was made

Types ​

DebounceHandler()<A> interface ​

Defined in: function/debounce/debounce.types.ts:11

The function that debounce returns, with methods to control it.

Type Parameters ​

A ​

A extends unknown[]

DebounceHandler(...args): void

Defined in: function/debounce/debounce.types.ts:13

Schedules fn to run after the wait. Each call restarts the wait and replaces the earlier arguments.

Parameters ​

args ​

...A

Returns ​

void

Properties ​

cancel ​

cancel: () => void

Defined in: function/debounce/debounce.types.ts:16

Clears a waiting call, so fn does not run.

Returns ​

void


flush ​

flush: () => void

Defined in: function/debounce/debounce.types.ts:19

Runs a waiting call immediately with its latest arguments. Does nothing if no call is waiting.

Returns ​

void


DebounceOptions interface ​

Defined in: function/debounce/debounce.types.ts:5

Options for debounce.

Properties ​

debounceMs? ​

optional debounceMs?: number

Defined in: function/debounce/debounce.types.ts:7

Delay in milliseconds after the last call before fn runs.

Default ​
ts
0