UNPKG

@ou-imdt/utils

Version:

Utility library for interactive media development

16 lines (15 loc) 672 B
/** * Creates a debounced version of the provided callback function. The debounced function delays the execution of the callback until after `delay` milliseconds have passed since the last time the debounced function was invoked. * * @param {Function} callback - The function to debounce. * @param {number} [delay=0] - The number of milliseconds to delay. Default = 0. * @returns {Function} A debounced version of the provided callback function. * */ export default function debounce(callback, delay = 0) { let timer; return function debouncedCallback(...args) { clearTimeout(timer); timer = setTimeout(() => callback.apply(this, args), delay); }; };