forked from rocketacademy/timer-bootcamp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
79 lines (69 loc) · 2.1 KB
/
script.js
File metadata and controls
79 lines (69 loc) · 2.1 KB
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
75
76
77
78
79
const milliSecOutput = document.getElementById(`milli-second`);
const secondOutput = document.getElementById(`second`);
const minuteOutput = document.getElementById(`minute`);
const hourOutput = document.getElementById(`hour`);
const lapResult = document.getElementById(`lap-data`);
let milliSecond = 0;
let second = 0;
let minute = 0;
let hour = 0;
let interval;
const buttonStart = document.getElementById(`start-button`);
const buttonStop = document.getElementById(`stop-button`);
const buttonReset = document.getElementById(`reset-button`);
const buttonLap = document.getElementById(`lap-button`);
buttonStart.addEventListener(`click`, () => {
interval = setInterval(startTimer, 10);
buttonStart.disabled = true;
});
buttonStop.addEventListener(`click`, () => {
clearInterval(interval);
buttonStart.disabled = false;
});
buttonReset.addEventListener(`click`, () => {
clearInterval(interval);
milliSecond = 0;
second = 0;
minute = 0;
hour = 00;
milliSecOutput.innerText = `000`;
secondOutput.innerText = `00 :`;
minuteOutput.innerText = `00 :`;
hourOutput.innerText = `00 :`;
lapResult.innerText = ``;
buttonStart.disabled = false;
});
buttonLap.addEventListener(`click`, () => {
const li = document.createElement(`li`);
li.innerText = `${hour} : ${minute} : ${second} : ${milliSecond}`;
lapResult.appendChild(li);
});
const startTimer = () => {
milliSecond++;
if (milliSecond < 9) {
milliSecOutput.innerText = `00${milliSecond}`;
}
if (milliSecond > 9 && milliSecond < 99) {
milliSecOutput.innerText = `0${milliSecond}`;
}
if (milliSecond > 99) {
second++;
milliSecond = 0;
secondOutput.innerText = ` ${second} : `;
}
if (second < 10) {
secondOutput.innerText = ` 0${second} : `;
}
if (second > 59) {
second = 0;
secondOutput.innerText = ` ${second} : `;
minute++;
minuteOutput.innerText = ` ${minute} : `;
}
if (minute > 59) {
minute = 0;
minuteOutput.innerText = ` ${minute} : `;
hour++;
minuteOutput.innerText = ` : ${hour} : `;
}
};