-
Notifications
You must be signed in to change notification settings - Fork 0
/
swatch.js
49 lines (42 loc) · 1.25 KB
/
swatch.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 timer;
let minutes = 0;
let seconds = 0;
let milliseconds = 0;
let lapCount = 1;
function start() {
timer = setInterval(displayTime, 10);
}
function stop() {
clearInterval(timer);
}
function reset() {
clearInterval(timer);
minutes = 0;
seconds = 0;
milliseconds = 0;
lapCount = 1;
document.getElementById("display").innerText = "00:00:00";
document.getElementById("lapList").innerHTML = "";
}
function lap() {
let lapTime = document.getElementById("display").innerText;
let lapItem = document.createElement("li");
lapItem.innerText = "Lap " + lapCount + ": " + lapTime;
document.getElementById("lapList").appendChild(lapItem);
lapCount++;
}
function displayTime() {
milliseconds++;
if (milliseconds == 100) {
milliseconds = 0;
seconds++;
}
if (seconds == 60) {
seconds = 0;
minutes++;
}
let displayMinutes = minutes < 10 ? "0" + minutes : minutes;
let displaySeconds = seconds < 10 ? "0" + seconds : seconds;
let displayMilliseconds = milliseconds < 10 ? "0" + milliseconds : milliseconds;
document.getElementById("display").innerText = `${displayMinutes}:${displaySeconds}:${displayMilliseconds}`;
}