-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
73 lines (63 loc) · 1.63 KB
/
Copy pathindex.js
File metadata and controls
73 lines (63 loc) · 1.63 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
const timeElement = document.getElementById("time")
let initTime = 0
let isRunning = false
let startTIme = null
const formatTime = (milliseconds) => {
const data = new Date(milliseconds * 1000)
const hours = data.getUTCHours().toString().padStart(2, "0")
const minutes = data.getUTCMinutes().toString().padStart(2, "0")
const seconds = data.getUTCSeconds().toString().padStart(2, "0")
return `${hours}:${minutes}:${seconds}`
}
const start = () => {
if (isRunning === false) {
timeElement.dataset.mode = ""
console.log("O time foi iniciado com sucesso!")
startTIme = setInterval(() => {
initTime += 1
const time = formatTime(initTime)
timeElement.textContent = time
}, 1000);
isRunning = true
}
}
const pause = () => {
if (isRunning === true) {
timeElement.dataset.mode = "red"
console.log("O time foi pausado com sucesso!")
clearInterval(startTIme)
isRunning = false
}
}
const reset = () => {
if (initTime > 0) {
timeElement.dataset.mode = ""
console.log("O time foi resetado com sucesso!")
timeElement.innerText = formatTime(0)
clearInterval(startTIme)
isRunning = false
initTime = 0
startTIme = null
}
}
async function startCronometro() {
const containerButtons = document.getElementById("buttons")
containerButtons.addEventListener("click", (e) => {
e.preventDefault()
const bottonclidado = e.target
switch (bottonclidado.id) {
case "start":
start()
break
case 'pause':
pause()
break
case "reset":
reset()
break
default:
return
}
})
}
startCronometro()