-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtimer.js
75 lines (62 loc) · 1.49 KB
/
timer.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
import Sounds from './sounds.js'
let { timeEnd } = Sounds()
export function Timer({ minutesDisplay, secondsDisplay, resetControls }) {
let timerTimeOut
let minutes = Number(minutesDisplay.textContent)
function updateDisplay(newMinutes, seconds) {
newMinutes = newMinutes === undefined ? minutes : newMinutes
seconds = seconds === undefined ? 0 : seconds
minutesDisplay.textContent = String(newMinutes).padStart(2, '0')
secondsDisplay.textContent = String(seconds).padStart(2, '0')
}
function countdown() {
timerTimeOut = setTimeout(function () {
let seconds = Number(secondsDisplay.textContent)
let minutes = Number(minutesDisplay.textContent)
let isFinished = minutes <= 0 && seconds <= 0
updateDisplay(minutes, 0)
if (isFinished) {
timeEnd()
resetControls()
updateDisplay()
return
}
if (seconds <= 0) {
seconds = 60
--minutes
}
--seconds
updateDisplay(minutes, seconds)
countdown()
}, 1000)
}
function plus() {
updateDisplay(
Number(minutesDisplay.textContent) + 1,
Number(secondsDisplay.textContent)
)
}
function minus() {
let minutes = Number(minutesDisplay.textContent)
let seconds = Number(secondsDisplay.textContent)
if (minutes > 1) {
updateDisplay(minutes - 1, seconds)
} else {
updateDisplay(0, seconds)
}
}
function reset() {
updateDisplay(minutes, 0)
clearTimeout(timerTimeOut)
}
function hold() {
clearTimeout(timerTimeOut)
}
return {
hold,
reset,
plus,
minus,
countdown
}
}