-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstopwatch.js
49 lines (42 loc) · 1.27 KB
/
stopwatch.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
38
39
40
41
42
43
44
45
46
47
48
49
let [milliseconds, seconds, minutes, hours] = [0, 0, 0, 0];
let timeRef = document.querySelector(".timer-display");
let int = null;
document.getElementById("start-timer").addEventListener("click", () => {
if(int !== null) {
clearInterval(int);
}
int = setInterval(displayTimer, 10);
});
document.getElementById("pause-timer").addEventListener("click", () => {
clearInterval(int);
});
document.getElementById("reset-timer").addEventListener("click", () => {
clearInterval(int);
[milliseconds, seconds, minutes, hours] = [0, 0, 0, 0];
timeRef.innerHTML = "00 : 00 : 00 : 000 ";
});
function displayTimer() {
milliseconds += 10;
if(milliseconds == 1000) {
milliseconds = 0;
seconds++;
if(seconds == 60) {
seconds = 0;
minutes++;
if(minutes == 60) {
minutes = 0;
hours++;
}
}
}
let h = hours < 10 ? "0" + hours : hours;
let m = minutes < 10 ? "0" + minutes : minutes;
let s = seconds < 10 ? "0" + seconds : seconds;
let ms =
milliseconds < 10
? "00" + milliseconds
: milliseconds < 100
? "0" + milliseconds
: milliseconds;
timeRef.innerHTML = `${h} : ${m} : ${s} : ${ms}`;
}