function debounce(func, n) { let timer = null; return (...args) => { clearTimeout(timer); timer = setTimeout(func, n, ...args); }; } function throttle(func, n) { let throttling = false; let last_args = null; let last_call = Date.now(); let timeout = null; return (...args) => { if (!throttling) { last_call = Date.now(); if (last_args) { func.apply(this, last_args); last_args = null; } else { func(...args); } throttling = true; if (timeout) { clearTimeout(timeout); } timeout = setTimeout( () => { throttling = false; if (last_args) { func.apply(this, last_args); } }, n - (Date.now() - last_call), ); } else { last_args = args; } }; } module.exports = { debounce, throttle, };