-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtimer.js
48 lines (46 loc) · 1.48 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
let timerCanceling = false
let intervalID, timer
let startTimer = (duration, display) => {
timer = duration
let minutes, seconds
return setInterval(function () {
minutes = parseInt(timer / 60, 10)
seconds = parseInt(timer % 60, 10)
minutes = minutes < 10 ? "0" + minutes : minutes
seconds = seconds < 10 ? "0" + seconds : seconds
display.textContent = `${minutes} : ${seconds}`
zeroTimer()
timer--
}, 1000)
}
let zeroTimer = () => {
if (timer == 0) {
clearInterval(intervalID)
let display = document.querySelector('#time')
let text = document.querySelector("#text")
text.innerText = "good job!"
display.textContent = "00 : 00"
}
}
window.onload = () => {
let threeMinutes = 60 * 3
let display = document.querySelector('#time')
intervalID = startTimer(threeMinutes, display)
}
window.onmousemove = () => {
clearInterval(intervalID)
let text = document.querySelector("#text")
text.innerText = "one more time!"
if (!timerCanceling) {
timerCanceling = true
setTimeout(function () {
let threeMinutes = 60 * 3 - 1
let display = document.querySelector('#time')
text.innerText = "keep calm for"
display.textContent = "03 : 00"
intervalID = startTimer(threeMinutes, display)
timerCanceling = false
}, 3000)
}
}
window.onkeypress = window.onmousedown = window.onmousemove