summaryrefslogtreecommitdiff
path: root/src/static/js/clocks.js
diff options
context:
space:
mode:
Diffstat (limited to 'src/static/js/clocks.js')
-rw-r--r--src/static/js/clocks.js45
1 files changed, 45 insertions, 0 deletions
diff --git a/src/static/js/clocks.js b/src/static/js/clocks.js
new file mode 100644
index 0000000..da617b6
--- /dev/null
+++ b/src/static/js/clocks.js
@@ -0,0 +1,45 @@
+const updateClock = (start, e) => {
+ let timeSince = new Date().getTime() - start;
+
+ let days = Math.floor(timeSince / (1000 * 60 * 60 * 24));
+ let hours = Math.floor(
+ (timeSince % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60),
+ );
+ let minutes = Math.floor((timeSince % (1000 * 60 * 60)) / (1000 * 60));
+ let seconds = Math.floor((timeSince % (1000 * 60)) / 1000);
+
+ const elems = e.getElementsByTagName("p");
+
+ for (const e of elems) {
+ switch (e.id) {
+ case "d":
+ e.innerHTML = days + "d";
+ break;
+ case "h":
+ e.innerHTML = hours + "h";
+ break;
+ case "m":
+ e.innerHTML = minutes + "m";
+ break;
+ case "s":
+ e.innerHTML = seconds + "s";
+ break;
+ default:
+ break;
+ }
+ }
+};
+
+const applyClock = (e) => {
+ const startattr = e.getAttributeNode("x-start-time");
+ if (startattr === null) return;
+
+ const start = Number.parseInt(startattr.value);
+
+ setInterval(() => updateClock(start, e), 1000);
+};
+
+const clocks = document.getElementsByClassName("clock");
+for (const e of clocks) {
+ applyClock(e);
+}