A sighted user sees "3 results", "Saved" or "Payment failed" appear on the page. A screen reader user hears nothing unless the page tells the screen reader. The tool is a live region: an element with aria-live, whose text changes are read out. It is easy to get wrong. A region created at the moment of the message is often not announced, display: none hides it from screen readers too, and writing the same text twice is not a change, so the second "Item added" is silent.
createAnnouncer() adds two empty regions to document.body straight away: one with aria-live="polite" and role="status", one with aria-live="assertive" and role="alert", both with aria-atomic="true".
The regions are visually hidden but not hidden from assistive technology: not display: none, not visibility: hidden, no hidden or aria-hidden; position: absolute and at most 1 by 1 pixel.
announce(message) empties the polite region at once and writes the message into it 100 ms later. announce(message, "assertive") does the same in the assertive region.
Announcing the same text again after it was written is a new change: the region is emptied first, then written again 100 ms later.
A newer message for a region replaces one that is still waiting, even when it is the same text: two calls within 100 ms give one write. Each region has its own pending message.
The message is written as text, never as HTML, after String(). An empty message only clears the region and schedules nothing.
Any politeness other than "polite" or "assertive" throws RangeError (but see rule 9).
clear() empties both regions and cancels pending messages.
destroy() removes both regions and cancels every timer. After it, every method does nothing and does not throw, whatever its arguments (an unknown politeness included).
constannouncer=createAnnouncer();searchBox.addEventListener("input", debounce(async () => {constresults=awaitsearch(searchBox.value);render(results); announcer.announce(`${results.length} results`); // read after the current speech}, 300));
JAVASCRIPT
addButton.addEventListener("click", () => { cart.add(item); announcer.announce("Item added to cart");});// Click, then click again a second later: the region is emptied and written// again, so a screen reader should read the message a second time.// Two calls within 100 ms are one write: the second replaces the first.
JAVASCRIPT
announcer.announce("Card declined", "assertive"); // meant to interrupt current speechannouncer.announce("x", "loud"); // RangeErrorannouncer.destroy();announcer.announce("x", "loud"); // does nothing after destroy
Plain JavaScript in a browser page; the tests call the global createAnnouncer from announcer.js.
Timers run on a virtual clock that replaces setTimeout and clearTimeout, and counts waiting timers.
The tests check the regions' attributes, computed styles, size and text. They cannot hear a screen reader: what is spoken, and when, is the intended result, and depends on the screen reader and browser. Check it by hand with one (for example: add an item twice, a second apart, and listen for the message both times).
Out of scope: queueing several messages so each is read in full, and clearing messages after a while.