function debounce(originalFn, timeoutMs) { let timeout; return (...args) => { clearTimeout(timeout); // clear timeout every time the function is called timeout = setTimeout(() => originalFn(...args), timeoutMs); // call the original function once "timeoutMs" ms after the last call have elapsed }; }