@@ -17,7 +17,22 @@ const start = (clock, template = clock.children[1]) => {
1717 * @param {Date } datetime - datetime to format
1818 * @return {string } the formatted datetime
1919 */
20- const formatTime = ( datetime ) => datetime . toLocaleTimeString ( 'en-GB' , { "hour12" : false } ) ;
20+ const formatTime = ( datetime ) => datetime . toLocaleTimeString ( 'en-GB' , { hour12 : false } ) ;
21+
22+ /**
23+ * Format the time to the minute, for the accessible text. Announcing every
24+ * second would flood assistive technology, so the text is deliberately no
25+ * more precise than the rate at which it is refreshed.
26+ *
27+ * @param {Date } datetime - datetime to format
28+ * @return {string } the formatted datetime
29+ */
30+ const formatMinute = ( datetime ) =>
31+ datetime . toLocaleTimeString ( 'en-GB' , {
32+ hour12 : false ,
33+ hour : '2-digit' ,
34+ minute : '2-digit' ,
35+ } ) ;
2136
2237 const time = formatTime ( new Date ( ) ) ;
2338
@@ -42,11 +57,11 @@ const start = (clock, template = clock.children[1]) => {
4257 if ( time !== lastTime ) {
4358 clock . setAttribute ( 'datetime' , date ) ;
4459
45- // Only update accessible text when the minute changes (not every second)
46- const currentMinute = time . substring ( 0 , 5 ) ; // "HH:MM"
47- if ( currentMinute !== lastMinute ) {
48- clock . children . item ( 0 ) . textContent = time ;
49- lastMinute = currentMinute ;
60+ const minute = formatMinute ( datetime ) ;
61+
62+ if ( minute !== lastMinute ) {
63+ clock . children . item ( 0 ) . textContent = minute ;
64+ lastMinute = minute ;
5065 }
5166
5267 for ( let i = 0 ; i < time . length ; i ++ ) {
@@ -56,8 +71,9 @@ const start = (clock, template = clock.children[1]) => {
5671 lastTime = time ;
5772 }
5873
59- requestAnimationFrame ( tick ) ;
74+ // Sleep until the next second boundary rather than waking every frame
75+ setTimeout ( tick , 1000 - ( Date . now ( ) % 1000 ) ) ;
6076 } ;
6177
62- requestAnimationFrame ( tick ) ;
78+ tick ( ) ;
6379} ;
0 commit comments