Skip to Content
Functiondebounce

debounce

The debounce function is commonly used to ensure that time-consuming tasks do not fire too often. It waits for a set interval after the last invocation before executing the callback function.

If immediate is set to true, the original function is called on the leading edge only. Further calls within the delay are ignored, and there is no trailing call after the delay. The next leading call can happen only after the delay has elapsed without invocations.

Import

import { debounce } from '@fullstacksjs/toolbox';

Signature

function debounce<F>( options: { delay: number; immediate?: boolean }, cb: F, ): (...args: Parameters<F>) => void {}

Examples

function log(id: number) { console.log(id); } const debounced = debounce({ delay: 1000 }, log); debounced(1); // ignored debounced(2); // ignored debounced(3); // ignored setTimeout(function () { debounced(4); // 4 debounced(5); // ignored }, 1000); const debounced = debounce({ delay: 1000, immediate: true }, log); debounced(1); // 1 debounced(2); // ignored debounced(3); // ignored setTimeout(function () { debounced(4); // 4 debounced(5); // ignored }, 1000);
Last updated on