Skip to content

Commit 113eb80

Browse files
committed
Switch exercise timer implementation to use luxon instead of JS dates.
1 parent 5ce6ffb commit 113eb80

File tree

1 file changed

+22
-22
lines changed

1 file changed

+22
-22
lines changed

slides/templates/regular/index.html.j2

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,8 @@ require(
166166
},
167167
[
168168
"{{ 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"
170171
],
171172
172173
function(Reveal, RevealNotes){
@@ -222,23 +223,21 @@ require(
222223
}
223224
);
224225
226+
// for working with dates/times
227+
const { DateTime, Duration } = luxon;
228+
225229
// add exercise timer
226230
let updateExerciseTimerInterval;
227231
Reveal.addKeyBinding(
228232
{ keyCode: 84, key: "T", description: "Start the exercise timer" },
229233
() => {
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')}`;
242241
243242
if ($('.exercise-timer').length === 0) { // add to DOM if not already there
244243
$('.reveal').append(
@@ -253,20 +252,21 @@ require(
253252
254253
// write the starting time minus the 1 second delay before the update
255254
const updateFrequency = 1000;
256-
$('.exercise-timer').text(getTimeText(exerciseTime - updateFrequency));
255+
$('.exercise-timer').text(getTimeText(exerciseTime.minus({ milliseconds: updateFrequency })));
257256
258257
// update the remaining time each second
259258
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
267267
audio.play();
268268
}
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');
270270
else {
271271
clearInterval(updateExerciseTimerInterval);
272272
$('.exercise-timer').remove();

0 commit comments

Comments
 (0)