Sub topic 2
Loading states and how waiting feels
This chapter assumes you know basic UI vocabulary (layout, components, copy). It also assumes you know how a web or mobile app fetches data asynchronously, and that you can read simple HTML and TypeScript. The previous chapter split a data-driven screen into loading, empty, error, and success states. Here we look at the loading state: what the user perceives while it is on screen, and how to design it so waiting feels short and predictable.
Why loading is a design problem
A loading state is the screen's answer to one question: "Did my action work, and is something still happening?" If the screen gives no answer, users guess. They tap again, reload, or leave.
The real duration of a request is only part of the story. Users judge the wait by what they perceive, and perception depends on feedback, expectation, and context. Two screens with the same 2-second wait can feel very different. One shows a blank white page. The other shows the page layout right away and fills in the content.
Design for perceived performance (how long the wait feels) as well as measured performance (how long it takes). You can improve perceived performance without touching the backend, so it is a design tool.
The three response-time ranges
Usability research, mainly from Jakob Nielsen, describes three rough thresholds. Treat them as guidelines, not exact limits.
| Wait | How it feels | What the interface should do |
|---|---|---|
| Up to about 0.1 s | Instant. The result seems caused directly by the user's action. | Show the result. No loading indicator. |
| About 0.1 s to 1 s | Noticeable, but the user's flow of thought continues. | Usually no indicator. Keep the previous content visible and avoid layout jumps. |
| About 1 s to 10 s | The user notices the delay and wants to know the system is working. | Show a clear indicator. Consider progress or placeholder content. |
| Over about 10 s | Attention drifts, and users may switch tasks. | Show determinate progress if possible, allow cancelling, and consider continuing in the background. |
Two practical results follow from this table. Very fast operations need no indicator, and showing one often makes the app feel slower. Long operations need more than a spinning icon, because a spinner gives no sense of how long is left.
Kinds of waiting you will design for
"Loading" covers several situations. They look different and need different treatment.
- Initial load. The screen has no data yet. There is nothing to keep on screen, so you show a placeholder or an indicator.
- Refresh or revalidation. The screen already has data and is fetching newer data. Keep the old content visible and signal the update quietly, for example with a small indicator in the toolbar. Replacing the whole screen with a spinner throws away information the user was reading.
- Loading more. A list is extending (pagination or infinite scroll). Add an indicator at the end of the list, not over it.
- Action in progress. The user pressed Save, Pay, or Send. Put the feedback at the control they used: disable it, change its label ("Saving…"), and keep the rest of the screen stable.
- Background work. An export, import, or upload takes minutes. Treat it as a task with its own status and let the user leave and come back.
A common error is to use one full-screen spinner for all five. Match the indicator's scope to the scope of the wait. If only the comments list is loading, the rest of the page should stay usable.
Why spinners often hurt
A spinner is cheap to build, and it is the right choice in some cases, covered in the next chapter. It also has costs.
- It carries no information about duration or progress.
- It does not show what the finished screen will look like, so the user waits without orientation.
- On fast responses, it appears and vanishes in a fraction of a second. That flash is more distracting than no indicator at all.
The last point is the one you will meet most often in real products. The fix is timing, not a different graphic.
Delay the indicator, then keep it visible long enough
Two timing rules prevent the flicker:
- Show delay. Do not show the indicator until the request has taken longer than a threshold, often 200 to 400 ms. If the response arrives first, the indicator never appears.
- Minimum display time. Once the indicator is visible, keep it visible for a short minimum, often 400 to 600 ms. Otherwise a response arriving at 320 ms would produce a flash that lasts 20 ms.
The helper below implements both. It is plain TypeScript with no dependencies, so you can paste it into a file and run it with a TypeScript runner such as tsx.
type Indicator = { show: () => void; hide: () => void };
function createDelayedIndicator(
indicator: Indicator,
delayMs = 300,
minVisibleMs = 500,
) {
let showTimer: ReturnType<typeof setTimeout> | undefined;
let hideTimer: ReturnType<typeof setTimeout> | undefined;
let shownAt: number | undefined;
return {
start(): void {
// A new request began while a hide was pending: keep showing.
if (hideTimer !== undefined) {
clearTimeout(hideTimer);
hideTimer = undefined;
return;
}
// Already waiting to show, or already visible.
if (showTimer !== undefined || shownAt !== undefined) return;
showTimer = setTimeout(() => {
showTimer = undefined;
shownAt = Date.now();
indicator.show();
}, delayMs);
},
stop(): void {
// Finished before the delay elapsed: the user never sees it.
if (showTimer !== undefined) {
clearTimeout(showTimer);
showTimer = undefined;
return;
}
if (shownAt === undefined || hideTimer !== undefined) return;
const elapsed = Date.now() - shownAt;
const remaining = Math.max(0, minVisibleMs - elapsed);
hideTimer = setTimeout(() => {
hideTimer = undefined;
shownAt = undefined;
indicator.hide();
}, remaining);
},
};
}
const spinner = createDelayedIndicator({
show: () => console.log("spinner on"),
hide: () => console.log("spinner off"),
});
const wait = (ms: number) => new Promise<void>((r) => setTimeout(r, ms));
async function load(label: string, ms: number): Promise<void> {
spinner.start();
try {
await wait(ms); // stands in for a network request
console.log(`${label} finished after ${ms} ms`);
} finally {
spinner.stop(); // runs on success and on failure
}
}
async function main(): Promise<void> {
await load("fast request", 100); // spinner never appears
await load("slow request", 1000); // spinner on at 300 ms, off at 1000 ms
}
main();Running this prints "fast request finished" with no spinner lines. It then prints "spinner on" and "spinner off" around "slow request finished".
Note the finally block. If the request fails and you only call stop() on success, the spinner stays on forever. This is one of the most common loading bugs. The failure should lead to an error state, and the indicator must be cleared either way.
Reserve the space to avoid layout shift
Waiting feels worse when the screen moves. If the loading indicator takes up 40 pixels and the content that replaces it takes 600, everything below jumps when the data arrives. A user who is about to tap a button may hit something else.
Reduce this by reserving space:
- Give the loading region a minimum height close to the expected content height.
- Give images and media fixed dimensions or an aspect ratio before they load.
- Keep headers, navigation, and toolbars outside the loading region so they never move.
The user should never lose their place on the screen because content arrived.
Setting expectations
How long users are willing to wait depends on what they expect. Some techniques that change expectations:
- Show structure early. Render the page frame, headings, and controls immediately, and load only the data region. The user sees that the page is "there" and that one part is still coming.
- Say what is happening. "Loading your projects…" is better than "Loading…". On longer waits, name the step: "Uploading photo 3 of 8".
- Show progress when you can measure it. A determinate indicator (one that shows a percentage or a count) tells the user how much is left. An indeterminate indicator only says "something is happening". Use determinate progress only when the number is real, because a bar that stalls at 90% damages trust.
- Give the user something to do. Content that arrives in pieces lets people start reading the first piece while the rest loads.
The next chapter covers how to choose between spinners, skeleton screens, progress bars, and optimistic updates. For now, remember the principle: the longer or less predictable the wait, the more information the indicator should carry.
Loading is a state, not a decoration
The indicator is the visible part of a state. Underneath it, the screen must be in a well-defined condition: loading, with no data yet, and with no error. A loading state that is mixed up with the others causes visible bugs.
Typical mistakes:
- Treating "no data yet" as "empty". The user briefly sees "No projects yet" before the list appears. This is wrong information, not just an ugly flash. An empty state should appear only after a successful response that contained no items.
- Leaving the loading state on after an error, so the spinner never ends.
- Starting a second request while the first is still running, and showing results from the older one after the newer one.
- Showing the loading state again on every background refresh, even when data is already on screen.
The discriminated union from the spine of this tutorial prevents the first two by construction, because a screen can hold only one status at a time.
type AsyncState<T> =
| { status: "loading" }
| { status: "error"; message: string; retryable: boolean }
| { status: "success"; data: T };
function renderList(state: AsyncState<string[]>): string {
switch (state.status) {
case "loading":
return "Loading projects…";
case "error":
return state.retryable
? `${state.message} Try again.`
: state.message;
case "success":
return state.data.length === 0
? "No projects yet. Create your first project."
: state.data.join(", ");
}
}Here "no projects yet" can only be returned inside the success case, so it cannot appear while data is still loading. The later chapter on modeling states in code returns to this pattern in more detail.
The loading lifecycle
This diagram shows the states from the user's point of view, including the delay and the minimum display time.
Settled stands for either the success state or the error state. Both are reached the same way, which is why the indicator cleanup belongs in a finally block.
Common errors
| Error | Effect on the user | Fix |
|---|---|---|
| Spinner shown for every request | Flicker on fast responses | Add a show delay and a minimum display time |
| Whole page replaced during a refresh | Loses reading position and context | Keep old content, show a small refresh indicator |
| Empty message shown before data arrives | User thinks they have no data | Show empty state only after a successful empty response |
| No cleanup on failure | Spinner never stops | Clear the indicator in `finally`, then show an error |
| Fake progress bar | Stalls at a fixed percentage, trust drops | Use indeterminate feedback unless progress is measurable |
| No reserved space | Layout shifts, mis-taps | Set minimum heights and media dimensions |
| Out-of-order responses | Stale data replaces fresh data | Cancel or ignore superseded requests |
Accessibility note
A visual indicator is invisible to a screen reader user. The loading state also needs a text equivalent that assistive technology announces, such as a polite status region. A later chapter covers the details of live regions, aria-busy, and focus management. For now, make sure every loading state you design has a text description, not only an animation.
Checklist for a loading state
- Which kind of wait is this: initial, refresh, loading more, action, or background?
- Is the expected duration under 1 second, in the 1 to 10 second range, or longer?
- Does the indicator have a show delay and a minimum display time?
- Does the indicator's scope match the part of the screen that is actually waiting?
- Is space reserved so that content arriving does not shift the layout?
- Is the indicator cleared on both success and failure?
- Can the "empty" message appear only after a real, successful response?
Exercise
A project list screen refreshes its data every 30 seconds. Currently each refresh replaces the list with a full-screen spinner, and when the response is fast the user sees a brief flash. Name two changes that fix this, and explain which kind of waiting this is.
Show answer
This is a refresh (revalidation), not an initial load, because the screen already has data. First, keep the existing list visible during the refresh and show at most a small, quiet indicator, such as one in the toolbar, so the user does not lose their reading position. Second, if any indicator is shown, add a show delay (for example 300 ms) and a minimum display time (for example 500 ms) so fast responses cause no flash. The full-screen spinner should be reserved for the initial load, when there is no data to keep.
Exercise
A developer writes spinner.start(); const data = await fetchProjects(); spinner.stop(); and users report that the spinner sometimes never goes away. What is the likely cause, and how do you fix it?
Show answer
If fetchProjects() throws, execution skips spinner.stop(), so the indicator stays on. Wrap the call in try { ... } finally { spinner.stop(); } so the indicator is cleared on both success and failure, and render an error state in the failure case.