-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
56 lines (51 loc) · 1.49 KB
/
index.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
50
51
52
53
54
55
56
let [milliseconds, seconds, minutes, hours] = [0, 0, 0, 0];
let timeRef = document.querySelector(".timer-display");
let int = null;
let lapCount = 1;
let lapList = document.getElementById("lap-list");
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 ";
});
document.getElementById("lap-timer").addEventListener("click", () => {
let lapTime = timeRef.innerHTML;
let lapItem = document.createElement("li");
lapItem.innerText = `Lap ${lapCount}: ${lapTime}`;
lapList.appendChild(lapItem);
lapCount++;
});
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}`;
}