aboutsummaryrefslogtreecommitdiff
path: root/timer.js
diff options
context:
space:
mode:
authorzlg <zlg@zlg.space>2023-04-01 23:41:02 -0700
committerzlg <zlg@zlg.space>2023-04-01 23:41:02 -0700
commit776605926111b462bba2d397b150b0447da8b6d9 (patch)
tree662e432e52faf395dcb6921971e92f4cdbcfa19d /timer.js
downloadzsst-776605926111b462bba2d397b150b0447da8b6d9.tar.gz
zsst-776605926111b462bba2d397b150b0447da8b6d9.tar.bz2
zsst-776605926111b462bba2d397b150b0447da8b6d9.tar.xz
zsst-776605926111b462bba2d397b150b0447da8b6d9.zip
Initial commit
This is a simple streaming timer meant to be hooked up to OBS and customized using a separate CSS file.
Diffstat (limited to 'timer.js')
-rw-r--r--timer.js49
1 files changed, 49 insertions, 0 deletions
diff --git a/timer.js b/timer.js
new file mode 100644
index 0000000..e2e9380
--- /dev/null
+++ b/timer.js
@@ -0,0 +1,49 @@
+let params;
+let cur_seconds = null;
+let timer_id;
+const cd = document.getElementById("countdown");
+
+function setupCountdown() {
+ params = new URLSearchParams(document.location.search);
+ for ([key, value] of params.entries()) {
+ console.log(`${key}: ${value}`);
+ }
+
+ 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");
+
+ 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();
+}
+
+function draw() {
+ document.getElementById("minutes").textContent = zero_pad(Math.floor(cur_seconds / 60));
+ document.getElementById("seconds").textContent = zero_pad((cur_seconds % 60));
+}
+
+function zero_pad(num) {
+ if (num < 10) {
+ return "0"+num;
+ } else {
+ return num;
+ }
+}