-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstopwatch.js
74 lines (56 loc) · 1.88 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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
// Convert time to a format of hours, minutes, seconds, and milliseconds
function timeS(time) {
let hrs = time / 3600000;
let hours = Math.floor(hrs);
let min = (hrs - hours) * 60;
let minutes = Math.floor(min);
let sec = (min - minutes) * 60;
let seconds = Math.floor(sec);
let Mis = (sec - seconds) * 100;
let ms = Math.floor(Mis);
let M = minutes.toString().padStart(2, "0");
let S = seconds.toString().padStart(2, "0");
let MS = ms.toString().padStart(2, "0");
return `${M}:${S}:${MS}`;
}
// Declare variables to use in our functions below
let startT;
let elapT = 0;
let intervalT;
// Create function to modify innerHTML
function print(cur) {
document.getElementById("display").innerHTML = cur;
}
// Create "start", "pause" and "reset" functions
function start() {
startT = Date.now() - elapT;
intervalT = setInterval(function printTime() {
elapT = Date.now() - startT;
print(timeS(elapT));
}, 10);
showButton("PAUSE");
}
function pause() {
clearInterval(intervalT);
showButton("PLAY");
}
function reset() {
clearInterval(intervalT);
print("00:00:00");
elapT = 0;
showButton("PLAY");
}
// Create function to display buttons
function showButton(buttonKey) {
const btnShow = buttonKey === "PLAY" ? playB : pauseB;
const btnHide = buttonKey === "PLAY" ? pauseB : playB;
btnShow.style.display = "block";
btnHide.style.display = "none";
}
// Create event listeners
let playB = document.getElementById("playBtn");
let pauseB = document.getElementById("pauseBtn");
let resetB = document.getElementById("resetBtn");
playB.addEventListener("click", start);
pauseB.addEventListener("click", pause);
resetB.addEventListener("click", reset);