@gecut/utilities
Version:
The ultimate utility toolkit from Gecut Company, crafted with TypeScript for optimal speed and efficiency. Designed to boost productivity with a suite of fast and optimized tools.
58 lines • 2.17 kB
JavaScript
import { nextAnimationFrame, nextIdleCallback, cancelNextAnimationFrame, cancelNextIdleCallback, supported, } from './wait/polyfill.js';
/**
* The `debounce` function takes a function and a delay as arguments and returns a new function that
* delays the execution of the original function until a certain amount of time has passed without any
* further invocations.
* @param func - The `func` parameter is a function that takes in any number of arguments of type
* `Args` and returns a value of type `Return`.
* @param {number} delay - The `delay` parameter is a number that represents the time in milliseconds
* to wait before invoking the `func` function.
* @returns The debounce function returns a new function that will execute the provided function (func)
* after a specified delay (delay) has passed.
*
* @example
* ```ts
* // Example usage
* function handleInput(value: string) {
* console.log(`Input value: ${value}`);
*
* return value;
* }
*
* const debouncedHandleInput = debounce(handleInput, 500);
*
* // Simulating user input
* debouncedHandleInput('First input');
* debouncedHandleInput('Second input');
* debouncedHandleInput('Third input');
*
* // Only the last input will be logged after a delay of 500ms
* ```
*/
export default function debounce(func, delay) {
if (delay === 'AnimationFrame' && supported.requestAnimationFrame && supported.cancelAnimationFrame) {
let timerId;
return (...args) => {
cancelNextAnimationFrame(timerId);
timerId = nextAnimationFrame(() => func(...args));
};
}
else if (delay === 'IdleCallback' && supported.requestIdleCallback && supported.cancelIdleCallback) {
let timerId;
return (...args) => {
cancelNextIdleCallback(timerId);
timerId = nextIdleCallback(() => func(...args));
};
}
else {
let timerId;
if (typeof delay !== 'number') {
delay = 0;
}
return (...args) => {
clearTimeout(timerId);
timerId = setTimeout(() => func(...args), delay);
};
}
}
//# sourceMappingURL=debounce.js.map