super-utils-plus
Version:
A superior alternative to Lodash with improved performance, TypeScript support, and developer experience
35 lines (34 loc) • 1.2 kB
TypeScript
import { DebounceOptions } from '../utils/types';
/**
* Creates a debounced function that delays invoking func until after wait
* milliseconds have elapsed since the last time the debounced function was
* invoked.
*
* The debounced function comes with a cancel method to cancel delayed func
* invocations and a flush method to immediately invoke them.
*
* @param func - The function to debounce
* @param wait - The number of milliseconds to delay
* @param options - The options object
* @returns The new debounced function
*
* @example
* ```ts
* // Avoid costly calculations while the window size is being changed
* window.addEventListener('resize', debounce(calculateLayout, 150));
*
* // Invoke `sendMail` when the click event is fired, debouncing subsequent calls
* const sendMailDebounced = debounce(sendMail, 300, {
* leading: true,
* trailing: false
* });
* element.addEventListener('click', sendMailDebounced);
*
* // Cancel the debounced function
* sendMailDebounced.cancel();
* ```
*/
export declare function debounce<T extends (...args: any[]) => any>(func: T, wait?: number, options?: DebounceOptions): T & {
cancel: () => void;
flush: () => void;
};