Question 66
Question
Answer
function debounce(func, delay) {
let timeoutId;
return function(...args) {
clearTimeout(timeoutId); // Clear any previous timeout
timeoutId = setTimeout(() => {
func.apply(this, args); // Call the original function after the delay
}, delay);
};
}
// Example usage:
const handleResize = () => {
console.log('Window resized!');
// Perform actions when window is resized
};
const debouncedResizeHandler = debounce(handleResize, 300); // Debounce with a delay of 300ms
window.addEventListener('resize', debouncedResizeHandler);Last updated