-
Notifications
You must be signed in to change notification settings - Fork 0
/
01-timer.html
132 lines (117 loc) · 3.23 KB
/
01-timer.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
<!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>1: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="2 1.666666666666667" 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 = 60;
// 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,
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>';
}
return '<p id="time-count">' + data.timer + '</p>';
};
/**
* 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();
};
/**
* Start the timer
*/
var startTimer = function () {
// Reset the data
data.timer = duration;
data.done = false;
data.offset = dOff;
// Run an initial render
render();
// Update the timer every second
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 and render new UI
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) {
window.clearInterval(countdown);
circle.style.strokeDashoffset = dOff;
}
}, 1000);
};
//
// Inits & Event Listeners
//
// Start the timer on page load
startTimer();
// When the restart button is clicked, restart the timer
document.addEventListener('click', function (event) {
if (event.target.hasAttribute('data-restart-timer')) {
startTimer();
}
}, false);
</script>
</body>
</html>