aboutsummaryrefslogtreecommitdiff
path: root/timer.js
blob: f625094938caf74508a717cec6dbeb561de95165 (plain)
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
let params;
let cur_seconds = null;
let timer_id;
let cd;

const page = document.getElementById("page");

function setupCountdown() {
	params = new URLSearchParams(document.location.search);
	for ([key, value] of params.entries()) {
		console.log(`${key}: ${value}`);
	}

	switch (params.get("cdstyle")) {
		case "standard":
			page.innerHTML = `
			<div id="countdown">
				<div id="minutes">00</div>
				<div id="separator">:</div>
				<div id="seconds">00</div>
			</div>
			`;
			break;
		case "bar":
			page.innerHTML = `
			<div id="bar"><div id="progress"></div>
			<div id="countdown">
				<div id="minutes">00</div>
				<div id="separator">:</div>
				<div id="seconds">00</div>
			</div>
			`;
			break;
	}
	cd = document.getElementById("countdown");
	cur_seconds = parseInt(params.get("duration"));

	if (params.get("f_family") !== "") {
		cd.style.fontFamily = params.get("f_family");
	}
	cd.style.color = params.get("f_color");
	if (params.get("cdstyle") == "bar") {
		document.getElementById("progress").style.backgroundColor = params.get("b_color");
	}

	draw();
	timer_id = window.setInterval(tick, 1000);
}

function tick() {
	if (cur_seconds == 0) {
		while (cd.firstChild) {
			cd.removeChild(cd.firstChild);
		}
		let end_msg = document.createElement("div");
		end_msg.textContent = params.get("end_text");
		cd.appendChild(end_msg);
		window.clearInterval(timer_id);
		return;
	}
	cur_seconds--;
	draw();
	if (params.get("cdstyle") == "bar") {
		draw_bar();
	}
}

function draw() {
	document.getElementById("minutes").textContent = zero_pad(Math.floor(cur_seconds / 60));
	document.getElementById("seconds").textContent = zero_pad((cur_seconds % 60));
}

function draw_bar() {
	document.getElementById("progress").style.width = (((params.get("duration") - cur_seconds) / params.get("duration")) * 100) + "vw";
}

function zero_pad(num) {
	if (num < 10) {
		return "0"+num;
	} else {
		return num;
	}
}