-
Notifications
You must be signed in to change notification settings - Fork 0
/
03-timer-start-pause.html
192 lines (159 loc) · 4.51 KB
/
03-timer-start-pause.html
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
180
181
182
183
184
185
186
187
188
189
190
191
192
<!DOCTYPE html>
<html>
<head>
<title>Timer</title>
<link rel="apple-touch-icon" sizes="180x180" href="apple-touch-icon.png">
<link rel="icon" type="image/png" sizes="32x32" href="favicon-32x32.png">
<link rel="icon" type="image/png" sizes="16x16" href="favicon-16x16.png">
<link rel="manifest" href="site.webmanifest">
<link rel="stylesheet" href="assets/css/styles.css">
</head>
<body>
<h1>Timer</h1>
<div class="timer">
<div id="total">
<p>2:00</p>
</div>
<div class="circle">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="4 4 74 74">
<circle class="dash-bk" r="34.44" cy="41" cx="41" stroke-width="5" />
<circle id="circle" class="circle_animation" r="34.44" cy="41" cx="41" stroke-width="5" />
<circle class="dash" r="34.44" cy="41" cx="41" stroke-width="6" stroke-dasharray="1 .833333333333333" stroke="midnightblue" />
</svg>
</div>
<div id="app"></div>
</div>
<script>
//
// Variables
//
// Get the #app element from the DOM
var app = document.querySelector('#app');
// Get the #circle svg element from the DOM
var circle = document.querySelector('#circle');
// Store duration to a variable
var duration = 120;
// Store circle offset value
var dOff = '220';
// Hide progress ring by setting to full offset value
circle.style.strokeDashoffset = dOff;
// The state/data object
var data = {
timer: duration,
done: false,
paused: true,
offset: dOff,
incr: dOff / duration
};
// A placeholder for the countdown interval
var countdown;
//
// Methods
//
/**
* Get the template markup
* @return {String} The HTML string
*/
var template = function () {
// If the timer is done, show a different UI
if (data.done) {
return '<button data-restart-timer aria-label="Restart Timer"></button>';
}
// Get the minutes and seconds
var minutes = parseInt(data.timer / 60, 10);
var seconds = data.timer % 60;
// Create the markup string
var html =
'<p id="time-count">' + minutes.toString() + ':' + seconds.toString().padStart(2, '0') + '</p>' +
'<div id="actions">' + (data.paused ? '<button data-start-timer aria-label="Start Timer"></button>' : '<button data-pause-timer aria-label="Pause Timer"></button>') + ' <button data-restart-timer aria-label="Restart Timer"></button></div>';
return html;
};
/**
* Render the template into the DOM
*/
var render = function () {
// If there are no updates to the UI, do nothing
if (app.innerHTML === template()) return;
// Update the UI
app.innerHTML = template();
};
/**
* Stop the countdown
*/
var stopCountdown = function () {
data.paused = true;
window.clearInterval(countdown);
};
/**
* Update the timer every second
*/
var startCountdown = function () {
data.paused = false;
countdown = window.setInterval(function () {
// Get the new timer value
var time = data.timer - 1;
// Get the new offset value
var prog = data.offset - data.incr;
// If the timer hits 0, set as done
var done = time === 0 ? true : false;
// Update data
data.timer = time;
data.done = done;
data.offset = prog;
circle.style.strokeDashoffset = data.offset;
// Render new UI
render();
// If the timer is done, stop it from running
if (done) {
stopCountdown();
circle.style.strokeDashoffset = dOff;
}
}, 1000);
};
/**
* Start the timer
*/
var startTimer = function () {
// Reset the data
data.timer = duration;
data.done = false;
data.offset = dOff;
// Clear any existing timers
stopCountdown();
// Update the timer every second
startCountdown();
// Run an initial render
render();
};
/**
* Handle click events
* @return {Event} The event object
*/
var clickHandler = function (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();
// Listen for clicks on the timer buttons
document.addEventListener('click', clickHandler, false);
</script>
</body>
</html>