@uswds/uswds
Version:
Open source UI components and visual style guide for U.S. government websites
19 lines (17 loc) • 561 B
JavaScript
/**
* Call a function every X amount of milliseconds.
*
* @param {Function} callback - A callback function to be debounced
* @param {number} delay - Milliseconds to wait before calling function
* @returns {Function} A debounced function
* @example const updateStatus = debounce((string) => console.log(string), 2000)
*/
module.exports = function debounce(callback, delay = 500) {
let timer = null;
return (...args) => {
window.clearTimeout(timer);
timer = window.setTimeout(() => {
callback.apply(this, args);
}, delay);
};
};