Sub topic 9
Timing: delays, flicker, and transitions
Assume you understand components, asynchronous data fetching, and basic TypeScript. This chapter focuses on when a screen changes state, not on the visual design of each state.
A loading indicator has two timing problems:
- If it appears too late, users may think the interface is broken.
- If it appears too quickly and disappears immediately, it creates flicker.
The right timing depends on the operation. A fast request needs almost no feedback. A long request needs a clear explanation of progress or delay.
Delay loading feedback to prevent flicker
Do not show a spinner for every request immediately. Instead, wait briefly before showing it. If the request finishes during that delay, the user sees the final result without an unnecessary flash.
A delay of about 200 to 300 milliseconds is a useful starting point. Test it with real interaction speed and device performance. The value is a design choice, not a universal rule.
function showAfterDelay(delayMs: number, show: () => void): () => void {
const id = setTimeout(show, delayMs);
return () => clearTimeout(id);
}
async function loadProjects(): Promise<string[]> {
const cancelSpinner = showAfterDelay(300, () => {
document.body.dataset.loading = "true";
});
try {
const response = await fetch("/api/projects");
if (!response.ok) {
throw new Error(`Request failed: ${response.status}`);
}
return await response.json() as string[];
} finally {
cancelSpinner();
delete document.body.dataset.loading;
}
}The cleanup must run whether the request succeeds or fails. Otherwise, a failed request can leave a loading indicator visible forever.
Avoid abrupt transitions
A state transition is the change from one screen state to another. Common transitions include:
- Loading to content
- Loading to empty
- Loading to error
- Content to refreshing content
- Content to error while keeping stale content visible
Use transitions to preserve context. For example, during a refresh, keep existing rows on screen and show a small “Updating…” indicator. Replacing the entire list with a blank spinner makes the interface feel slower and can move the user’s focus.
Do not animate every state change. Animation should clarify continuity, not delay access to content. Keep motion short, avoid shifting controls, and respect the user’s reduced-motion preference.
[data-loading="true"] .refresh-indicator {
opacity: 1;
transition: opacity 150ms ease-in;
}
.refresh-indicator {
opacity: 0;
transition: opacity 150ms ease-out;
}
@media (prefers-reduced-motion: reduce) {
.refresh-indicator {
transition: none;
}
}Match the pattern to the wait
Choose feedback based on what users know about the operation.
| Situation | Suitable feedback | Timing concern |
|---|---|---|
| Unknown wait under a few seconds | Delayed spinner | Prevent a short flash |
| Content has a known layout | Skeleton screen | Do not hide useful existing content |
| Measurable work | Progress indicator | Show honest progress |
| Small, reversible update | Optimistic update | Explain or undo if it fails |
A skeleton is useful when the page structure is known and the delay is long enough to notice. It is less useful when the response is usually immediate. A progress bar is appropriate only when progress can be measured or estimated honestly. A bar that pauses at 90 percent damages trust.
Optimistic UI updates the interface before the server confirms success. Use it for actions such as starring an item or toggling a setting when reversal is safe. Keep a recovery path if the request fails. Do not use optimism for destructive or irreversible actions unless the consequences are clear.
Model the transition sequence
Write down the expected sequence before implementing it. This exposes unwanted paths, such as an error appearing after a newer successful request.
Cancel timers and ignore obsolete responses when a user starts a newer request. Without this protection, a slow first request can overwrite the result of a faster second request.
Review timing with realistic conditions
Test on a fast connection, a slow connection, an offline device, and a response that fails after loading begins. Also test repeated navigation and refresh actions. Check whether content jumps, whether focus moves unexpectedly, and whether a loading announcement is repeated too often.
Exercise
A search request usually finishes in 100 milliseconds but sometimes takes 2 seconds. Which timing strategy should you start with, and why?
Show answer
Delay the spinner by roughly 200 to 300 milliseconds. Fast requests finish without flicker, while slower requests still receive visible feedback. Keep the search results or previous results visible when possible, and handle an obsolete response if the query changes.