Debounce
Also called debouncing or debounced search
A technique that waits for a short pause in typing or other repeated events, then runs the work once instead of on every keystroke.
import { useEffect, useState } from 'react';
const DELAY = 300; // ms of quiet before searching
type Result = { id: string; title: string };
function useDebouncedValue<T>(value: T, delay: number) {
const [debounced, setDebounced] = useState(value);
useEffect(() => {
// Every keystroke clears the timer and starts the wait again.
const id = setTimeout(() => setDebounced(value), delay);
return () => clearTimeout(id);
}, [value, delay]);
return debounced;
}
export function SearchBox() {
const [query, setQuery] = useState('');
const [results, setResults] = useState<Result[]>([]);
const debouncedQuery = useDebouncedValue(query.trim(), DELAY);
useEffect(() => {
if (debouncedQuery.length < 2) return;
// Cancel the previous request so older results never replace newer ones.
const controller = new AbortController();
fetch(`/api/search?q=${encodeURIComponent(debouncedQuery)}`, {
signal: controller.signal
})
.then((response) => response.json() as Promise<Result[]>)
.then(setResults)
.catch((error: unknown) => {
if (!controller.signal.aborted) console.error(error);
});
return () => controller.abort();
}, [debouncedQuery]);
return (
<div className='w-80'>
<input
type='search'
value={query}
onChange={(event) => setQuery(event.target.value)}
placeholder='Search projects'
aria-label='Search projects'
className='h-10 w-full rounded-lg border bg-background px-3 text-sm'
/>
{query.trim().length >= 2 && (
<ul className='mt-2 rounded-lg border p-1 text-sm empty:hidden'>
{results.map((result) => (
<li key={result.id} className='rounded-md px-3 py-2 hover:bg-muted'>
{result.title}
</li>
))}
</ul>
)}
</div>
);
}Debounce the search box: wait until the user has stopped typing for 300ms before sending the search request, and skip the request when the query is shorter than 2 characters. Cancel any in-flight request with an AbortController when a new one starts, so older results never replace newer ones, and show a small spinner in the input while a request runs.
Paste into Layerwise or any AI coding tool.
What is Debounce?
Typing "running shoes" into a search box takes 13 keystrokes. Without a debounce, a live search sends 13 requests, most of them for half-typed words like "runn". A debounce starts a short timer on each keystroke and restarts it on the next one. The work runs only when the timer finishes, which means the person paused.
Use it for search as you type, autocomplete, autosave, server-side validation, and window resize handlers. Delays of 200–400ms feel responsive for search; autosave can wait a second or more.
Debounce is often mixed up with throttle. Throttle runs the work at most once per interval while events keep coming, which suits scroll and drag handlers that need steady updates; debounce waits for the events to stop. Also cancel or ignore older requests, so a slow early response can’t replace a newer one.
Related terms
All termsInline validation
Form checking that happens right next to each field as you fill it in, showing an error or a checkmark before you press Submit.
Combobox
A text field with a dropdown list that filters as you type, so people can pick one option from a long list quickly.
Command palette
A search box that opens with a shortcut like ⌘K and lets you find and run any command or page by typing a few letters.
Loading state
The view a screen shows while its data is on the way, designed together with the outcomes that follow: loaded, empty, or error.
Build it with Layerwise
Describe your app, watch it run in a live preview, and publish it with a database, storage, and sign-in built in.