-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathteatimer.js
59 lines (51 loc) · 1.64 KB
/
teatimer.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
function parseSeconds() {
var splitURL = window.location.toString().split("/");
var combinedTime = splitURL[splitURL.length - 1].split("m");
var minutes = parseInt(combinedTime[0]) || 0;
var seconds = parseInt(combinedTime[1]) || 0;
if (combinedTime.length > 2) { return NaN; }
if (minutes === 0 && seconds === 0) { return NaN; }
if (combinedTime.length === 1) { return Math.abs(60 * minutes); }
return Math.abs((60 * minutes) + seconds);
}
function chime(audio) {
audio.play();
alert("your tea is ready!");
}
function tick(audio) {
var countdown = document.getElementById("countdown");
var seconds = parseInt(countdown.innerText) - 1;
if (seconds > 0) {
countdown.innerText = seconds;
document.title = "teatimer: " + seconds;
} else {
countdown.innerText = "your tea is ready!";
document.title = "your tea is ready!";
clearInterval(window.timerInterval);
chime(audio);
}
}
function startTimer() {
this.style.display = "none";
var audio = new Audio("chime.mp3");
window.timerInterval = setInterval(tick, 1000, audio);
}
function main() {
var seconds = parseSeconds();
if (isNaN(seconds)) {
window.location.replace("http://teatimer.site");
return;
}
var countdown = document.createElement("span");
countdown.id = "countdown";
countdown.innerText = seconds;
var content = document.getElementById("content");
var body = content.parentNode;
body.replaceChild(countdown, content);
var startButton = document.createElement("button");
startButton.id = "startButton";
startButton.onclick = startTimer;
startButton.innerText = "start timer";
body.appendChild(startButton);
}
main();