-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
63 lines (53 loc) · 1.81 KB
/
script.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
let totalCentiSeconds = 0
let totalSeconds = 0;
let totalMinutes = 0;
let totalHours = 0;
//Test cases
//let s55 = 55 * 100;
//let m59 = 59 * 60 * 100;
//let h23 = 23 * 60 * 60 * 100;
//totalCentiSeconds = s55;
//totalCentiSeconds = m59 + s55;
//totalCentiSeconds = h23 + m59 + s55;
let timeoutID;
const hoursLabel = document.querySelector("#hours");
const minutesLabel = document.querySelector("#minutes");
const secondsLabel = document.querySelector("#seconds");
const centiSecondsLabel = document.querySelector("#centi-seconds");
const startButton = document.querySelector("#start");
const resetButton = document.querySelector("#reset");
startButton.addEventListener("click", startStopwatch);
resetButton.addEventListener("click", resetStopwatch);
function startStopwatch() {
if (this.dataset.status === "off") {
//If the value is less than 10, the value 10 is used
timeoutID = setInterval(setTime, 10);
this.textContent = "Stop";
this.dataset.status = "on";
} else {
clearTimeout(timeoutID);
this.textContent = "Start";
this.dataset.status = "off";
}
}
function setTime() {
//if (totalCentiSeconds >= 24 * 60 * 60 * 100) {totalCentiSeconds = 0;}
totalCentiSeconds++;
totalSeconds = parseInt(totalCentiSeconds / 100);
totalMinutes = parseInt(totalSeconds / 60);
totalHours = parseInt(totalMinutes / 60);
centiSecondsLabel.textContent = pad(totalCentiSeconds % 100);
secondsLabel.textContent = pad(totalSeconds % 60);
minutesLabel.textContent = pad(totalMinutes % 60);
hoursLabel.textContent = pad(totalHours);
}
function pad(value) {
return (value > 9) ? value : ("0" + value);
}
function resetStopwatch() {
totalCentiSeconds = 0;
centiSecondsLabel.textContent = pad(0);
secondsLabel.textContent = pad(0);
minutesLabel.textContent = pad(0);
hoursLabel.textContent = pad(0);
}