-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
70 lines (62 loc) · 1.74 KB
/
script.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
class Pomodoro {
constructor() {
this.buttons = {
start: $('#start'),
reset: $('#reset'),
sessionUp: $('#sessionup'),
sessionDown: $('#sessiondown')
}
this.sessionInput = $('#sessioninput')
this.minutes = 14
this.countdown
this.playing = false
this.registerEvents()
}
registerEvents() {
this.buttons.sessionDown.on('click', () => {
if (this.playing === true) {
return
}
if (this.minutes > 1) {
this.minutes--
$('#minutes').html(this.minutes < 10 ? '0' + this.minutes : this.minutes)
}
})
this.buttons.sessionUp.on('click', () => {
if (this.playing === true) {
return
}
if (this.minutes < 50) {
this.minutes++
$('#minutes').html(this.minutes < 10 ? '0' + this.minutes : this.minutes)
}
})
this.buttons.reset.on('click', () => {
$('#minutes').html(this.minutes < 10 ? '0' + this.minutes : this.minutes)
$('#seconds').html('00')
this.playing = false
clearInterval(this.countdown)
})
this.buttons.start.on('click', () => {
const duration = this.minutes * 60
this.startCount(duration)
})
}
startCount(duration) {
this.playing = true
this.countdown = setInterval(() => {
let minutes = parseInt(duration / 60, 10)
let seconds = parseInt(duration % 60, 10)
minutes = minutes < 10 ? '0' + minutes : minutes
seconds = seconds < 10 ? '0' + seconds : seconds
$('#minutes').html(minutes)
$('#seconds').html(seconds)
if (--duration < 0) {
clearInterval(this.countdown)
this.playing = false
return
}
}, 1000)
}
}
const clock = new Pomodoro()