Skip to content

Commit a05c5f0

Browse files
authored
Internalize role="timer" codepen demo (#39729)
1 parent 85e9fe9 commit a05c5f0

File tree

1 file changed

+58
-1
lines changed
  • files/en-us/web/accessibility/aria/reference/roles/timer_role

1 file changed

+58
-1
lines changed

files/en-us/web/accessibility/aria/reference/roles/timer_role/index.md

Lines changed: 58 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,64 @@ Along with [`alert`](/en-US/docs/Web/Accessibility/ARIA/Reference/Roles/alert_ro
3535

3636
If a time limit needs to be in place, for example, for security reasons, the user should have the option to turn it off or extend it. This restriction does not apply if the time limit is due to a live event, such as an auction or a game, or if the time to complete the form is essential for a valid submission.
3737

38+
## Examples
39+
40+
### A basic timer
41+
42+
This example has a timer that counts down from 30 seconds to 0 seconds. The whole time display region has `role="time"`, and also [`aria-atomic`](/en-US/docs/Web/Accessibility/ARIA/Reference/Attributes/aria-atomic) to indicate that the region should be announced as a whole, not just the changed regions. Due to the implicit `aria-live="off"`, changes are not announced by default; we manually set it to `"assertive"` when the timer reaches 10 seconds remaining so it gets announced once.
43+
44+
```html
45+
<div id="countdown" role="timer" aria-atomic="true">
46+
<span id="number">30</span> seconds left!
47+
</div>
48+
```
49+
50+
```css
51+
html {
52+
font-size: 50px;
53+
text-align: center;
54+
margin-top: 1em;
55+
font-family: sans-serif;
56+
}
57+
58+
#number {
59+
font-family: monospace;
60+
color: #cc0000;
61+
font-weight: bold;
62+
font-size: 1.25em;
63+
vertical-align: middle;
64+
}
65+
```
66+
67+
```js
68+
const numElement = document.getElementById("number");
69+
const liveRegion = document.getElementById("countdown");
70+
let startTime = new Date().getTime();
71+
72+
function decrement() {
73+
const timeNow = new Date().getTime();
74+
const elapsedTime = Math.floor((timeNow - startTime) / 1000);
75+
let newNumber = 30 - elapsedTime;
76+
77+
if (newNumber >= 0) {
78+
numElement.textContent = newNumber;
79+
}
80+
81+
if (newNumber === 10) {
82+
liveRegion.setAttribute("aria-live", "assertive");
83+
liveRegion.setAttribute("role", "alert");
84+
setTimeout(() => {
85+
liveRegion.removeAttribute("aria-live");
86+
liveRegion.setAttribute("role", "timer");
87+
}, 999);
88+
}
89+
}
90+
91+
window.setInterval(() => {
92+
decrement();
93+
}, 500);
94+
```
95+
3896
## Specifications
3997

4098
{{Specifications}}
@@ -46,4 +104,3 @@ If a time limit needs to be in place, for example, for security reasons, the use
46104
- [ARIA: `marquee` role](/en-US/docs/Web/Accessibility/ARIA/Reference/Roles/marquee_role)
47105
- [ARIA: `status` role](/en-US/docs/Web/Accessibility/ARIA/Reference/Roles/status_role)
48106
- [ARIA live regions](/en-US/docs/Web/Accessibility/ARIA/Guides/Live_regions)
49-
- [`timer` example on CodePen](https://codepen.io/heydon/pres/NGgNjZ) by Heydon Pickering

0 commit comments

Comments
 (0)