/** * A debounce function that delays the execution of a given function until after a specified delay. * It helps to prevent a function from being called too frequently, particularly in response to user input or events. * * @param {Function} func - The function to be debounced. * @param {number} delay - The delay in milliseconds after which the function should be executed. * @returns {Function} - A debounced version of the provided function. */ export default function debounce(func: Function, delay: number) { let debounceTimeout: any; /** * The returned function that wraps the original function and applies the debounce logic. * It cancels any previously scheduled invocation of the function and sets a new timeout. * * @param {...any[]} args - The arguments to pass to the debounced function. */ return function (...args: any[]) { // Clear the previous timeout, ensuring only the last call within the delay period is executed clearTimeout(debounceTimeout); // Set a new timeout to call the function after the specified delay debounceTimeout = setTimeout(() => func(...args), delay); }; }