es-next-tools
Version:
A comprehensive utility library for JavaScript and TypeScript that provides a wide range of functions for common programming tasks, including mathematical operations, date manipulations, array and object handling, string utilities, and more.
19 lines (18 loc) • 690 B
JavaScript
/**
* Creates a debounced function that delays invoking the original function until after `wait` milliseconds have elapsed since the last time it was invoked.
* @param {T} fn - The function to debounce.
* @param {number} wait - The number of milliseconds to delay.
* @returns {Function} A new, debounced function.
* @template T
* @example
* const debouncedFn = debounce(() => console.log('Debounced'), 300);
* // Calling debouncedFn multiple times rapidly will only log once after 300ms of inactivity
*/
export function debounce(fn, wait) {
let timeout;
return (...args) => {
clearTimeout(timeout);
timeout = setTimeout(() => fn(...args), wait);
};
}
;