@@ -166,7 +166,8 @@ require(
166
166
},
167
167
[
168
168
" {{ reveal_url_prefix }}/dist/reveal.js" ,
169
- " {{ reveal_url_prefix }}/plugin/notes/notes.js"
169
+ " {{ reveal_url_prefix }}/plugin/notes/notes.js" ,
170
+ " https://cdn.jsdelivr.net/npm/luxon@3.2.1/build/global/luxon.min.js"
170
171
],
171
172
172
173
function (Reveal , RevealNotes ){
@@ -222,23 +223,21 @@ require(
222
223
}
223
224
);
224
225
226
+ // for working with dates/times
227
+ const { DateTime , Duration } = luxon;
228
+
225
229
// add exercise timer
226
230
let updateExerciseTimerInterval;
227
231
Reveal .addKeyBinding (
228
232
{ keyCode: 84 , key: " T" , description: " Start the exercise timer" },
229
233
() => {
230
- const input = prompt (" How many minutes?" );
231
- if (input !== undefined && input !== " " && input !== null ){
232
- if (! isNaN (input) && input >= 0.1 ) {
233
- const exerciseTime = input * 60 * 1000 ;
234
- const countDownTime = new Date (new Date ().getTime () + exerciseTime).getTime ();
235
-
236
- const getTimeText = (elapsedTime ) => {
237
- const minutes = Math .floor ((elapsedTime % (1000 * 60 * 60 )) / (1000 * 60 ));
238
- const seconds = Math .floor ((elapsedTime % (1000 * 60 )) / 1000 );
239
- const padInt = (x ) => String (x).padStart (2 , ' 0' );
240
- return ` ⏱ ${ padInt (minutes)} :${ padInt (seconds)} ` ;
241
- }
234
+ let exerciseTime = prompt (" Timer duration in minutes" );
235
+ if (exerciseTime !== undefined && exerciseTime !== " " && exerciseTime !== null ){
236
+ if (! isNaN (exerciseTime) && exerciseTime >= 0.1 ) {
237
+ exerciseTime = Duration .fromMillis (exerciseTime * 60 * 1000 );
238
+ const countDownTime = DateTime .now ().plus (exerciseTime);
239
+
240
+ const getTimeText = (elapsedTime ) => ` ⏱ ${ elapsedTime .toFormat (' mm:ss' )} ` ;
242
241
243
242
if ($ (' .exercise-timer' ).length === 0 ) { // add to DOM if not already there
244
243
$ (' .reveal' ).append (
@@ -253,20 +252,21 @@ require(
253
252
254
253
// write the starting time minus the 1 second delay before the update
255
254
const updateFrequency = 1000 ;
256
- $ (' .exercise-timer' ).text (getTimeText (exerciseTime - updateFrequency));
255
+ $ (' .exercise-timer' ).text (getTimeText (exerciseTime . minus ({ milliseconds : updateFrequency }) ));
257
256
258
257
// update the remaining time each second
259
258
const updateTimer = () => {
260
- const now = new Date ().getTime ();
261
- const elapsedTime = countDownTime - now;
262
-
263
- if (elapsedTime >= 1000 ) $ (' .exercise-timer' ).text (getTimeText (elapsedTime));
264
- else if (elapsedTime >= 0 ) {
265
- $ (' .exercise-timer' ).text (getTimeText (0 ));
266
- const audio = new Audio (' ../../media/time_is_up.m4a' ); // TODO switch to URL of hosted version
259
+ const now = DateTime .now ();
260
+ const elapsedTime = countDownTime .diff (now);
261
+ const secondsRemaining = elapsedTime .as (' seconds' )
262
+
263
+ if (secondsRemaining >= 1 ) $ (' .exercise-timer' ).text (getTimeText (elapsedTime));
264
+ else if (secondsRemaining >= 0 ) {
265
+ $ (' .exercise-timer' ).text (getTimeText (Duration .fromMillis (0 )));
266
+ const audio = new Audio (' ../../media/time_is_up.m4a' ); // TODO: switch to URL of hosted version
267
267
audio .play ();
268
268
}
269
- else if (elapsedTime >= - 5 * 1000 ) $ (' .exercise-timer' ).text (" TIME'S UP" ).css (' color' , ' red' );
269
+ else if (secondsRemaining >= - 5 ) $ (' .exercise-timer' ).text (" TIME'S UP" ).css (' color' , ' red' );
270
270
else {
271
271
clearInterval (updateExerciseTimerInterval);
272
272
$ (' .exercise-timer' ).remove ();
0 commit comments