-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
88 lines (85 loc) · 2.47 KB
/
app.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
// Containers
const timeFunction = document.querySelectorAll(".time-function")
const milliSecondsContainer = document.querySelector("#milli-seconds")
const secondsContainer = document.querySelector("#seconds")
const minuteContainer = document.querySelector("#minutes")
const hourContainer = document.querySelector("#hour")
let clearState
// Counter Variables
let milliSecondCounter = 0
let secondsCounter = 0
let minuteCounter = 0
let hourCounter = 0
hover()
start()
stop()
reset()
function reset() {
for (let i = 0; i < timeFunction.length; i++) {
timeFunction[i].addEventListener("click", (event) => {
let element = timeFunction[i].classList.contains("reset")
if (element) {
milliSecondCounter = 0
secondsCounter = 0
minuteCounter = 0
hourCounter = 0
milliSecondsContainer.textContent = "00"
secondsContainer.textContent = "00"
minuteContainer.textContent = "00"
hourContainer.textContent = "00"
}
})
}
}
function stop() {
for (let i = 0; i < timeFunction.length; i++) {
timeFunction[i].addEventListener("click", (event) => {
let element = timeFunction[i].classList.contains("stop")
if (element) {
clearInterval(clearState)
}
})
}
}
function start() {
for (let i = 0; i < timeFunction.length; i++) {
timeFunction[i].addEventListener("click", (event) => {
let element = timeFunction[i].classList.contains("start")
if (element) {
clearState = setInterval(() => {
milliSecondCounter++
milliSecondsContainer.textContent = milliSecondCounter
if (milliSecondCounter >= 100) {
milliSecondCounter = 0
secondsCounter++
secondsContainer.textContent = secondsCounter
}
if (secondsCounter >= 60) {
secondsCounter = 0
minuteCounter++
minuteContainer.textContent = minuteCounter
}
if (minuteCounter >= 60) {
minuteCounter = 0
hourCounter++
hourContainer.textContent = hourCounter
}
}, 10)
}
})
}
}
function hover() {
for (let i = 0; i < timeFunction.length; i++) {
timeFunction[i].addEventListener("click", (event) => {
let stat = event.target
for (let j = 0; j < timeFunction.length; j++) {
if (i == j) {
stat.classList.add("hover")
} else {
timeFunction[j].classList.remove("hover")
}
}
})
}
}