UNPKG

@techmely/utils

Version:

Collection of helpful JavaScript / TypeScript utils

28 lines (24 loc) 550 B
/*! * @techmely/utils * Copyright(c) 2021-2024 Techmely <techmely.creation@gmail.com> * MIT Licensed */ // src/debounce/index.ts function debounce(func, delay, immediate = false) { let timeoutId; return function(...args) { const callNow = immediate && !timeoutId; if (timeoutId) clearTimeout(timeoutId); timeoutId = setTimeout(() => { timeoutId = null; if (!immediate) { func.apply(this, args); } }, delay); if (callNow) { func.apply(this, args); } }; } export { debounce };