-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
47 lines (37 loc) · 1.19 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
let startBtn = document.querySelector('#start').addEventListener ('click', toStart,);
let pauseBtn = document.querySelector('#pause').addEventListener ('click', toPause);
let stopBtn = document.querySelector('#stop').addEventListener ('click', toStop);
let hour = 0;
let min = 0;
let sec = 0;
let ms = 0;
const time = 10;
let stopwatchActivity = 'none';
let stopwatch;
function toStart() {
if (stopwatchActivity == 'none') {
stopwatch = setInterval(() => { timer() }, time);
stopwatchActivity = 'active';
} else {}
}
function toPause() {
clearInterval(stopwatch);
stopwatchActivity = 'none';
}
function toStop() {
stopwatchActivity = 'none';
clearInterval(stopwatch);
hour = 0;
min = 0;
sec = 0;
ms = 0
document.querySelector('#timer').innerHTML = '00:00:00<span id="millisecond">:00</span>';
}
function timer() {
ms++;
if (ms == 100) { ms = 0; sec++ }
if (sec == 60) { sec = 0; min++; }
if (min == 60) { min = 0; hour++; }
let visualTimer = `${(hour < 10 ? `0${hour}` : hour)}:${(min < 10 ? `0${min}` : min)}:${(sec < 10 ? `0${sec}` : sec)}<span id="millisecond">:${ms < 10 ? `0${ms}` : ms}</span>`;
document.querySelector('#timer').innerHTML = visualTimer;
}