-
Notifications
You must be signed in to change notification settings - Fork 0
/
timer.js
37 lines (32 loc) · 921 Bytes
/
timer.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
let paused = true;
let startTime = null;
let timer = null;
document.querySelector("#timer").innerText = "1:30";
const startTimer = () => {
paused = false;
startTime = new Date().getTime() + 90000;
};
const updateTimer = () => {
timer = Math.max(startTime - new Date().getTime(), 0);
const minutes = Math.floor(timer / 60000);
const seconds = Math.floor((timer % 60000) / 1000);
const formattedTimer = `${minutes}:${seconds < 10 ? `0${seconds}` : seconds}`;
if (formattedTimer != document.querySelector("#timer").innerText) {
document.querySelector("#timer").innerText = formattedTimer;
}
};
const stopTimer = () => {
paused = true;
};
const makeTimer = (onTimeDone) => {
console.log("starting timer");
startTimer();
const intervalId = setInterval(() => {
updateTimer();
if (timer <= 0) {
stopTimer();
clearInterval(intervalId);
onTimeDone();
}
}, 120);
};