Skip to main content
This release is 5 versions behind 1.33.0 — the latest version of @es-toolkit/es-toolkit. Jump to latest

@es-toolkit/es-toolkit@1.29.0-dev.958+1d1d0ee6
Built and signed on GitHub Actions

A modern JavaScript utility library that's 2-3 times faster and up to 97% smaller—a major upgrade to lodash.

This package works with Cloudflare Workers, Node.js, Deno, Bun, Browsers
This package works with Cloudflare Workers
This package works with Node.js
This package works with Deno
This package works with Bun
This package works with Browsers
JSR Score
100%
Published
3 months ago (1.29.0-dev.958+1d1d0ee6)
function debounce
debounce<F extends (...args: any[]) => any>(
func: F,
debounceMs?: number,
options?: DebounceOptions,
): DebouncedFunction<F>

Creates a debounced function that delays invoking the provided function until after debounceMs milliseconds have elapsed since the last time the debounced function was invoked. The debounced function also has a cancel method to cancel any pending execution.

You can set the debounced function to run at the start (leading) or end (trailing) of the delay period. If leading is true, the function runs immediately on the first call. If trailing is true, the function runs after debounceMs milliseconds have passed since the last call. If both leading and trailing are true, the function runs at both the start and end, but it must be called at least twice within debounceMs milliseconds for this to happen (since one debounced function call cannot trigger the function twice).

You can also set a maxWait time, which is the maximum time the function is allowed to be delayed before it is called.

Examples

const debouncedFunction = debounce(() => { console.log('Function executed'); }, 1000);

// Will log 'Function executed' after 1 second if not called again in that time debouncedFunction();

// Will not log anything as the previous call is canceled debouncedFunction.cancel();

// With AbortSignal const controller = new AbortController(); const signal = controller.signal; const debouncedWithSignal = debounce(() => { console.log('Function executed'); }, 1000, { signal });

debouncedWithSignal();

// Will cancel the debounced function call controller.abort();

Type Parameters

F extends (...args: any[]) => any
  • The type of function.

Parameters

func: F
  • The function to debounce.
optional
debounceMs: number = 0
  • The number of milliseconds to delay.
optional
options: DebounceOptions
  • The options object

Return Type

A new debounced function with a cancel method.

Add Package

deno add jsr:@es-toolkit/es-toolkit

Import symbol

import { debounce } from "@es-toolkit/es-toolkit/compat";

---- OR ----

Import directly with a jsr specifier

import { debounce } from "jsr:@es-toolkit/es-toolkit/compat";

Add Package

npx jsr add @es-toolkit/es-toolkit

Import symbol

import { debounce } from "@es-toolkit/es-toolkit/compat";

Add Package

yarn dlx jsr add @es-toolkit/es-toolkit

Import symbol

import { debounce } from "@es-toolkit/es-toolkit/compat";

Add Package

pnpm dlx jsr add @es-toolkit/es-toolkit

Import symbol

import { debounce } from "@es-toolkit/es-toolkit/compat";

Add Package

bunx jsr add @es-toolkit/es-toolkit

Import symbol

import { debounce } from "@es-toolkit/es-toolkit/compat";