Sub topic 12
Latency, Debouncing, and Feedback
Assume you know HTML forms, asynchronous requests, and basic TypeScript. This chapter uses those ideas to make search and filtering feel responsive without wasting server or browser resources.
Latency is part of the interface
Latency is the time between a user action and the visible response. A search request may take 50 milliseconds on a fast connection and several seconds on a slow one. Users need to know whether the interface accepted their action, is still working, or failed.
Design explicit states:
- Idle: no request is running.
- Loading: a request is in progress.
- Success: results are visible.
- Empty: the request succeeded but found nothing.
- Error: the request failed.
- Stale: an older response must not replace newer results.
A small loading indicator can appear inside the results region. Keep existing results visible when possible. Replacing useful content with a blank spinner makes the interface feel slower.
Debounce input-driven requests
A search-as-you-type interface can create one request per keystroke. Debouncing waits until the user pauses before calling the search function. The delay is commonly between 200 and 400 milliseconds, but test it with real users and network conditions.
function debounce<A extends unknown[]>(
fn: (...args: A) => void,
waitMs: number
): (...args: A) => void {
let timer: ReturnType<typeof setTimeout> | undefined;
return (...args: A) => {
if (timer !== undefined) clearTimeout(timer);
timer = setTimeout(() => {
fn(...args);
}, waitMs);
};
}
const requestSuggestions = debounce((query: string) => {
if (query.trim().length < 2) return;
console.log(`fetch suggestions for "${query}"`);
}, 250);
requestSuggestions("ca");
requestSuggestions("cat");Debouncing is appropriate when intermediate values are not useful. Do not debounce actions that users expect to happen immediately, such as pressing a submit button or selecting a checkbox that updates results instantly.
Prevent stale responses
Debouncing reduces requests, but it does not guarantee that responses return in order. A request for ca may finish after a later request for cat. Use an AbortController or request identifier to prevent old results from overwriting new ones.
let activeController: AbortController | undefined;
async function search(query: string): Promise<void> {
activeController?.abort();
activeController = new AbortController();
const response = await fetch(`/api/search?q=${encodeURIComponent(query)}`, {
signal: activeController.signal,
});
if (!response.ok) throw new Error(`Search failed: ${response.status}`);
const results: string[] = await response.json();
console.log("render", results);
}Handle AbortError as normal cancellation, not as a failure message. Show an error only when the current request fails for another reason.
Choose feedback by delay
A response under roughly 100 milliseconds feels immediate. Between 100 and 1,000 milliseconds, a progress cue helps. For longer operations, show an informative loading state and preserve the user's query and filters.
Exercise
Why can debouncing alone still allow incorrect search results to appear?
Show answer
It delays requests but does not control response order. An older request can finish after a newer request, so cancellation or a request identifier is also needed.