Question: ๐๐ฆ๐ฉ๐ฅ๐๐ฆ๐๐ง๐ญ ๐ ๐๐๐๐จ๐ฎ๐ง๐๐ ๐ ๐ฎ๐ง๐๐ญ๐ข๐จ๐ง ๐จ๐ซ ๐๐ซ๐ข๐ญ๐ ๐ ๐๐๐๐จ๐ฎ๐ง๐๐ ๐๐ฎ๐ง๐๐ญ๐ข๐จ๐ง ๐ญ๐ก๐๐ญ ๐๐๐ฅ๐๐ฒ๐ฌ ๐ข๐ง๐ฏ๐จ๐ค๐ข๐ง๐ ๐ ๐๐ฎ๐ง๐๐ญ๐ข๐จ๐ง ๐ฎ๐ง๐ญ๐ข๐ฅ ๐๐๐ญ๐๐ซ ๐ง ๐ฆ๐ข๐ฅ๐ฅ๐ข๐ฌ๐๐๐จ๐ง๐๐ฌ ๐ก๐๐ฏ๐ ๐๐ฅ๐๐ฉ๐ฌ๐๐ ๐ฌ๐ข๐ง๐๐ ๐ญ๐ก๐ ๐ฅ๐๐ฌ๐ญ ๐ญ๐ข๐ฆ๐ ๐ญ๐ก๐ ๐๐ฎ๐ง๐๐ญ๐ข๐จ๐ง ๐ฐ๐๐ฌ ๐ข๐ง๐ฏ๐จ๐ค๐๐.
Answer:
function debounce(func, delay) { let timeoutId; return function (...args) { clearTimeout(timeoutId); timeoutId = setTimeout(() => { func.apply(this, args); }, delay); }; }
const debouncedFunction = debounce(() => { console.log('Debounced function invoked'); }, 300);
debouncedFunction();
// This call will be delayed by 300msQuestion: ๐๐ฆ๐ฉ๐ฅ๐๐ฆ๐๐ง๐ญ ๐๐ก๐ซ๐จ๐ญ๐ญ๐ฅ๐ ๐ ๐ฎ๐ง๐๐ญ๐ข๐จ๐ง ๐จ๐ซ ๐๐ซ๐ข๐ญ๐ ๐ ๐ญ๐ก๐ซ๐จ๐ญ๐ญ๐ฅ๐ ๐๐ฎ๐ง๐๐ญ๐ข๐จ๐ง ๐ญ๐ก๐๐ญ ๐ฅ๐ข๐ฆ๐ข๐ญ๐ฌ ๐ญ๐ก๐ ๐ข๐ง๐ฏ๐จ๐๐๐ญ๐ข๐จ๐ง ๐จ๐ ๐ ๐๐ฎ๐ง๐๐ญ๐ข๐จ๐ง ๐ญ๐จ ๐จ๐ง๐๐ ๐๐ฏ๐๐ซ๐ฒ ๐ง ๐ฆ๐ข๐ฅ๐ฅ๐ข๐ฌ๐๐๐จ๐ง๐๐ฌ.
Answer:
function throttle(func, limit) { let inThrottle; return function (...args) { if (!inThrottle) { func.apply(this, args); inThrottle = true; setTimeout(() => { inThrottle = false; }, limit); } }; }
const throttledFunction = throttle(() => { console.log('Throttled function invoked'); }, 300);
throttledFunction();
// This call will be limited to once every 300ms
ย