ms milliseconds have elapsed since its last invocation.clearTimeout(). Use setTimeout() to create a new timeout that delays invoking the function until at least ms milliseconds have elapsed.Function.prototype.apply() to apply the this context to the function and provide the necessary arguments.ms, to set the timeout at a default of 0 ms.const debounce = (fn, ms = 0) => {
let timeoutId;
return function(...args) {
clearTimeout(timeoutId);
timeoutId = setTimeout(() => fn.apply(this, args), ms);
};
};
window.addEventListener(
'resize',
debounce(() => {
console.log(window.innerWidth);
console.log(window.innerHeight);
}, 250)
); // Will log the window dimensions at most every 250msSubscribe to get resources directly to your inbox. You won't receive any spam! ✌️