-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtimer.html
More file actions
210 lines (167 loc) · 4.89 KB
/
timer.html
File metadata and controls
210 lines (167 loc) · 4.89 KB
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<meta charset="utf-8">
<title>Silent Countdown Timer</title>
<!--
This code is licensed under CC0 “No Rights Reserved”. It was written by Johan Mattsson.
The picture was taken by Andy Bear Grills Barker: https://www.pexels.com/@andy-bear-grills-barker-565823 (the picture is also free to use).
-->
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<link rel="icon" type="image/png" href="images/timerfavicon2.png" id="favicon">
<script>
let counter = 10;
let state = "stopped";
window.onload = function() {
let time = localStorage.getItem('totalTime');
if (time != null) {
document.getElementById("totalTime").value = time;
}
updateTotalCounter();
}
document.addEventListener('DOMContentLoaded', function() {
if (!Notification) {
return;
}
if (Notification.permission !== 'granted') {
Notification.requestPermission();
}
});
function updateTotalCounter() {
let time = document.getElementById("totalTime").value;
counter = parseInt(time * 60);
localStorage.setItem('totalTime', time);
updateCounter();
}
function showTimeoutNotification() {
let message = document.getElementById("message").value;
if (Notification.permission !== 'granted') {
Notification.requestPermission().then(function (permission) {
if (permission === "granted") {
new Notification(message, { body: message });
}
});
} else {
let message = document.getElementById("message").value;
new Notification(message, { body: message });
}
}
function updateFavicon() {
var favicon = document.querySelector("#favicon");
if (counter < 0) {
if (counter % 2) {
favicon.href = "images/timerfavicon1.png";
} else {
favicon.href = "images/timerfavicon2.png";
}
} else {
favicon.href = "images/timerfavicon1.png";
}
}
function updateCounter() {
const seconds_per_hour = 60 * 60;
const seconds_per_minute = 60;
let negative = counter < 0;
let currentCounter = negative ? -counter : counter;
let hours = parseInt(currentCounter / seconds_per_hour);
let minutes = parseInt((currentCounter % seconds_per_hour) / seconds_per_minute);
let seconds = parseInt((currentCounter - seconds_per_hour * hours - minutes * seconds_per_minute) % seconds_per_minute);
if (hours > 0 && hours < 10) {
hours = "0" + hours;
}
if (minutes < 10) {
minutes = "0" + minutes;
}
if (seconds < 10) {
seconds = "0" + seconds;
}
let sign = negative ? "-" : "";
let fullTime = sign;
if (hours > 0) {
fullTime += hours + ":";
}
fullTime += minutes + ":" + seconds;
document.getElementById("countdown").innerHTML = fullTime;
updateFavicon();
}
function iteration() {
if (state == "stopped" || state == "paused") {
return;
}
counter--;
updateCounter();
if (counter == 0) {
showTimeoutNotification();
}
}
function run() {
if (state == "running") {
document.getElementById("startButton").innerHTML = "Continue";
pause();
return;
}
if (typeof timer != "undefined") {
clearInterval(timer);
}
if (state != "paused") {
counter = parseInt(document.getElementById("totalTime").value * 60);
}
state = "running"
document.getElementById("startButton").innerHTML = "Pause";
timer = setInterval(iteration, 1000);
updateCounter();
}
function pause() {
state = "paused"
}
function resume() {
state = "running"
}
function reset() {
pause();
document.getElementById("startButton").innerHTML = "Start";
counter = parseInt(document.getElementById("totalTime").value * 60);
updateCounter();
}
</script>
<style>
body {
font-family: sans-serif;
background: url(images/robin-bg.jpg) no-repeat center center fixed;
background-size: cover;
background-color: #000;
padding-left: 20px;
}
button {
margin-right: 10px;
color: #fff;
background-color: #000;
border: none;
width: 100px;
padding: 10px 10px 10px 10px;
}
input {
padding-left: 5px;
}
#countdown {
font-size: 52px;
color: #fff;
}
.settings {
color: #fff;
width: 350px;
}
</style>
</head>
<body>
<div id="countdown" style="font-size: 72pt">30:00</div>
<div class="settings">
Time: <input type="number" id="totalTime" value="30" style="width: 50px;" oninput="updateTotalCounter()"> minutes<br><br>
Message: <input type="text" id="message" value="Time is up."><br><br>
<button onclick="run()" id="startButton">Start</button>
<button onclick="reset()">Reset</button><br>
<p>This is a silent countdown timer. It will display a notification when the time is up but it will not annoy your coworkers by making a sound.</p>
</div>
</body>
</html>