-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
58 lines (50 loc) · 1.53 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
57
58
let timer = null;
let msec = 0,
sec = 0,
min = 0,
hr = 0,
intervalId;
function Stopwatch() {
msec++;
if(msec == 100) {
msec = 0;
sec++;
if(sec==60) {
sec=0;
min++;
if(min==60) {
min=0;
hr++;
}
}
}
const milliseconds = msec.toString().padStart(2, "0");
const seconds = sec.toString().padStart(2, "0");
const minutes = min.toString().padStart(2, "0");
const hours = hr.toString().padStart(2, "0");
document.getElementById("hr").innerHTML = hours;
document.getElementById("min").innerHTML = minutes;
document.getElementById("sec").innerHTML = seconds;
document.getElementById("msec").innerHTML = milliseconds;
}
function startStopwatch() {
if(timer !== null) {
clearInterval(timer);
}
timer = setInterval(Stopwatch,10)
}
const stopStopwatch = () => {
clearInterval(timer);
};
const resetStopwatch = () => {
clearInterval(timer);
msec = 0, sec = 0, min = 0, hr = 0;
const milliseconds = msec.toString().padStart(2, "0");
const seconds = sec.toString().padStart(2, "0");
const minutes = min.toString().padStart(2, "0");
const hours = hr.toString().padStart(2, "0");
document.getElementById("hr").innerHTML = hours;
document.getElementById("min").innerHTML = minutes;
document.getElementById("sec").innerHTML = seconds;
document.getElementById("msec").innerHTML = milliseconds;
};