From a22bb66b0aa6da2f42baeba4c334696fbe617341 Mon Sep 17 00:00:00 2001 From: Alexis Werefox Date: Mon, 26 Apr 2021 05:09:36 +0000 Subject: [PATCH] Functionality on the HRT tracker is done. --- src/info/pages/hrt/index.js | 37 ++++++++++++++++++++++++++--- src/info/public/js/hrttimer.js | 43 ++++++++++++++++++++++++++++++++++ 2 files changed, 77 insertions(+), 3 deletions(-) create mode 100644 src/info/public/js/hrttimer.js diff --git a/src/info/pages/hrt/index.js b/src/info/pages/hrt/index.js index 46f5fdb..8f236c9 100644 --- a/src/info/pages/hrt/index.js +++ b/src/info/pages/hrt/index.js @@ -1,19 +1,50 @@ import BasicPage from "../../components/basic-page-template"; import WCard from "../../components/werefox-card"; +function getTimes(interval) { + let expected = Date.now() - new Date("December 11, 2020 00:00:00") + interval; + let days = Math.round(expected / 1000 / 60 / 60 / 24); + let hours = (new Date(expected).getHours() + 6) % 24; + let minutes = new Date(expected).getMinutes(); + let seconds = new Date(expected).getSeconds(); + return { + expected: expected, + days: days, + hours: hours, + minutes: minutes, + seconds: seconds, + }; +} + export default function HRT() { + const initialTimes = getTimes(0); + const initialTimesArray = [ + `${initialTimes["days"]} days, `, + `${initialTimes["hours"]} hours, `, + `${initialTimes["minutes"]} minutes, `, + `and ${initialTimes["seconds"]} seconds`, + ]; + console.log(initialTimesArray); + return ( -

- Oh no I haven't actually finished this page but I started on +

+ I'm so glad you're interested!!
- December 11th, 2020 + I have been on HRT for: +
+

+ {initialTimesArray.map((t, n) => ( +

{t}

+ ))} +

+
); } diff --git a/src/info/public/js/hrttimer.js b/src/info/public/js/hrttimer.js new file mode 100644 index 0000000..be88c59 --- /dev/null +++ b/src/info/public/js/hrttimer.js @@ -0,0 +1,43 @@ +window.onload = function HRTTimer() { + let interval = 1000; + let times = getTimes(interval); + let expected = times["expected"]; + setTimeout(step, interval); + function step() { + const dt = Date.now() - expected; // the drift (positive for overshooting) + if (dt > interval) { + // something really bad happened. Maybe the browser (tab) was inactive? + // possibly special handling to avoid futile "catch up" run + } + times["days"] = Math.round(expected / 1000 / 60 / 60 / 24); + times["hours"] = (new Date(expected).getHours() + 6) % 24; + times["minutes"] = new Date(expected).getMinutes(); + times["seconds"] = new Date(expected).getSeconds(); + if (document.getElementById("time_0") != null) { + document.getElementById("time_0").textContent = times["days"] + " days, "; + document.getElementById("time_1").textContent = + times["hours"] + " hours, "; + document.getElementById("time_2").textContent = + times["minutes"] + " minutes, "; + document.getElementById("time_3").textContent = + "and " + times["seconds"] + " seconds"; + } + expected += interval; + setTimeout(step, Math.max(interval - dt, 1000)); // take into account drift + } +}; + +function getTimes(interval) { + let expected = Date.now() - new Date("December 11, 2020 00:00:00") + interval; + let days = Math.floor(expected / 1000 / 60 / 60 / 24); + let hours = new Date(expected).getHours(); + let minutes = new Date(expected).getMinutes(); + let seconds = new Date(expected).getSeconds(); + return { + expected: expected, + days: days, + hours: hours, + minutes: minutes, + seconds: seconds, + }; +}