-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
44 lines (40 loc) · 1.27 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
function stopwatch(){
let currentState
let seconds = 0
let minutes = 0
let hours = 0
const secObj = document.getElementById("seconds")
const minObj = document.getElementById("minutes")
const hourObj = document.getElementById("hours")
document.getElementById("start-button").addEventListener("click", () => {
if(currentState == "running") return;
currentState = "running"
runTimeCycle()
})
document.getElementById("stop-button").addEventListener("click", () => { currentState = "stopped" })
document.getElementById("reset-button").addEventListener("click", () => { currentState = 'reset'; setTime(); })
runTimeCycle = () => setTimeout(setTime, 1000)
function setTime(){
if(currentState === "running"){
seconds++
if(seconds === 60){
minutes++
seconds = 0
if(minutes === 60){
hours++
minutes = 0
}
}
}
if(currentState === 'reset'){
hours = 0
minutes = 0
seconds = 0
}
secObj.textContent = seconds < 10 ? '0' + seconds.toString() : seconds
minObj.textContent = minutes < 10 ? '0' + minutes.toString() : minutes
hourObj.textContent = hours < 10 ? '0' + hours.toString() : hours
currentState === 'running' && runTimeCycle()
}
}
stopwatch()