CodShot
JavaScript Public

Debounce Function

A reusable debounce helper for search inputs and resize handlers.

#javascript #debounce #search #utility
JavaScript
function debounce(callback, delay = 300) {
  let timerId;

  return function (...args) {
    clearTimeout(timerId);

    timerId = setTimeout(() => {
      callback.apply(this, args);
    }, delay);
  };
}

const handleSearch = debounce((event) => {
  console.log('Search:', event.target.value);
}, 300);

document.querySelector('#search').addEventListener('input', handleSearch);

Notes

Debounce is useful when you do not want to run code on every single keystroke.

Snippet actions

CodShot user
CodShot user
Updated: 2026-06-03 14:05
Public snippets
0
Forks
CodShot AI Help
Ask about CodShot features, plans, workspace, AI limits, referrals, and public help topics.
Hi! I can help you understand how CodShot works. What would you like to know?
For account-specific or sensitive issues, please open a support ticket. Open support