-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
179 lines (147 loc) · 3.77 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
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
;(function() {
'use strict'
//
// Variables
//
// Get the #app element from the DOM
const app = document.querySelector('#app')
// Store duration to a variable
var duration = 120
// The state/data object
const data = {
timer: duration,
done: false,
paused: true
}
// A placeholder for the countdown interval
let countdown
//
// Methods
//
/**
* String.prototype.padStart() polyfill
* https://github.com/uxitten/polyfill/blob/master/string.polyfill.js
* https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/padStart
*/
if (!String.prototype.padStart) {
String.prototype.padStart = function padStart(targetLength, padString) {
targetLength = targetLength >> 0 //truncate if number or convert non-number to 0;
padString = String(typeof padString !== 'undefined' ? padString : ' ')
if (this.length > targetLength) {
return String(this)
} else {
targetLength = targetLength - this.length
if (targetLength > padString.length) {
padString += padString.repeat(targetLength / padString.length) //append to original to ensure we are longer than needed
}
return padString.slice(0, targetLength) + String(this)
}
}
}
const formatTimer = _ => {
// Get the minutes and seconds
const minutes = parseInt(data.timer / 60, 10)
const seconds = data.timer % 60
const paddedMinutes = minutes.toString()
const paddedSeconds = seconds.toString().padStart(2, '0')
return paddedMinutes + ':' + paddedSeconds
}
/**
* Get the template markup
* @return {String} The HTML string
*/
const template = _ => {
// If the timer is done, show a different UI
if (data.done) {
return '<p>⏰</p><button data-restart-timer>Restart Timer</button>'
}
// Create the markup string
const html =
`<p>${formatTimer(data.timer)}</p>` +
'<p>' +
(data.paused
? '<button data-start-timer>Start</button>'
: '<button data-pause-timer>Pause</button>') +
' <button data-restart-timer>Restart</button></p>'
return html
}
/**
* Render the template into the DOM
*/
const render = _ => {
// If there are no updates to the UI, do nothing
if (app.innerHTML === template()) return
// Update the UI
app.innerHTML = template()
}
/**
* Stop the countdown
*/
const stopCountdown = _ => {
data.paused = true
clearInterval(countdown)
}
/**
* Update the timer every second
*/
const startCountdown = _ => {
data.paused = false
countdown = setInterval(function() {
// Get the new timer value
let time = data.timer - 1
// If the timer hits 0, set as done
const done = time === 0 ? true : false
// Update data
data.timer = time
data.done = done
// Render new UI
render()
// If the timer is done, stop it from running
if (data.done) {
stopCountdown()
}
}, 1000)
}
/**
* Start the timer
*/
const startTimer = _ => {
// Reset the data
data.timer = duration
data.done = false
// Clear any existing timers
stopCountdown()
// Update the timer every second
startCountdown()
// Run an initial render
render()
}
/**
* Handle click events
* @return {Event} The event object
*/
const clickhandler = event => {
// If a restart button was clicked, start the timer
if (event.target.hasAttribute('data-restart-timer')) {
startTimer()
}
// If the start button was clicked, start the timer
// Restart the countdown
if (event.target.hasAttribute('data-start-timer')) {
startCountdown()
render()
}
// If the pause button was clicked, pause the timer
// Stop the countdown
if (event.target.hasAttribute('data-pause-timer')) {
stopCountdown()
render()
}
}
//
// Inits & Event Listeners
//
// Render the timer on page load
render()
document.addEventListener('click', clickhandler, false)
})()