@andranik-arakelyan/js-utilities
Version:
Javascript utilities
27 lines (26 loc) • 1.12 kB
TypeScript
/**
* Creates a debounced function that delays invoking the provided function until after
* the specified wait time has elapsed since the last time it was invoked.
* The debounced function will only execute once the wait period has passed without any new calls.
* Each new invocation resets the timer.
*
* @param func - The function to debounce
* @param wait - The number of milliseconds to delay
* @returns A debounced function that delays invoking the provided function
*
* @example
* // Create a debounced version of a function
* const handleInput = debounce((value) => {
* // This will only execute after the user has stopped typing for 300ms
* console.log('Processing input:', value);
* }, 300);
*
* // Call the debounced function
* inputElement.addEventListener('input', e => handleInput(e.target.value));
*
* // The debounced function can be cancelled to prevent pending execution
* // handleInput.cancel();
*/
export declare function debounce<T extends (...args: any[]) => any>(func: T, wait: number): ((...args: Parameters<T>) => ReturnType<T> | undefined) & {
cancel: () => void;
};