// Created on savesnippets.com ยท https://savesnippets.com/Kzur5C7GmBSiNO function debounce(fn, wait = 300) { let timer; return function (...args) { clearTimeout(timer); timer = setTimeout(() => fn.apply(this, args), wait); }; } // Usage const onSearch = debounce((e) => { console.log('Searching:', e.target.value); }, 400); document.querySelector('#search').addEventListener('input', onSearch);