CodShot
JavaScript Public

Debounced Search Input Handler

Implements a debounced search input handler that delays API calls until the user stops typing for 500ms, reducing unnecessary requests.

JavaScript
function debounce(func, delay = 300) {
    let timeout;

    return function (...args) {
        clearTimeout(timeout);
        timeout = setTimeout(() => {
            func.apply(this, args);
        }, delay);
    };
}

const searchInput = document.getElementById('search');

function handleSearch(event) {
    const query = event.target.value;

    if (query.length < 2) return;

    console.log("Searching for:", query);

    fetch(`/api/search?q=${encodeURIComponent(query)}`)
        .then(res => res.json())
        .then(data => console.log(data))
        .catch(err => console.error(err));
}

// lkdjáfdádjf -----------------

const debouncedSearch = debounce(handleSearch, 500);

searchInput.addEventListener('input', debouncedSearch);

Snippet actions

Pluto
Pluto
Updated: 2026-09-01 16: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