From 7b2067f1a3a2b2ffb0e71df0537309119e7d41ad Mon Sep 17 00:00:00 2001 From: That_One_Nerd Date: Mon, 1 May 2023 09:49:56 -0400 Subject: [PATCH] added exam timer. summary soon --- index.html | 1 + js/index.js | 63 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 64 insertions(+) create mode 100644 js/index.js diff --git a/index.html b/index.html index 73c3f46..b9eae07 100644 --- a/index.html +++ b/index.html @@ -89,4 +89,5 @@ + diff --git a/js/index.js b/js/index.js new file mode 100644 index 0000000..96d0be1 --- /dev/null +++ b/js/index.js @@ -0,0 +1,63 @@ +const title = document.getElementById("title"); + +// This is for the AP exam timer. +const examStart = new Date("May 3, 2023 12:00"); +const examEnd = new Date("May 3, 2023 15:15"); + +const examElement = document.createElement("h3"); +if (title != undefined) +{ + // Add the newly created AP exam countdown element. + title.append(examElement); + + // Enable the AP exam countdown. + updateCountdown(); + setInterval(updateCountdown, 1000); +} + +function updateCountdown() +{ + // Difference between now and the desired date in milliseconds. + const now = new Date(Date.now()); + var mode = "waiting"; + + var difference = examStart - now; + if (difference < 0) + { + difference = examEnd - now; + mode = "doing"; + } + if (difference < 0) + { + difference = -difference; + mode = "done"; + } + + // Get minutes and seconds from decimal hours. + var deciHours = difference / (1000 * 60 * 60); + var deciMinutes = (deciHours % 1) * 60; + var deciSeconds = (deciMinutes % 1) * 60; + + var hours = Math.floor(deciHours); + var minutes = Math.floor(deciMinutes); + var seconds = Math.floor(deciSeconds); + + // Construct string and assign it. + var string = `${hours}:${("0" + minutes).slice(-2)}:${("0" + seconds).slice(-2)}`; + switch (mode) + { + case "waiting": + string += " until the AP exam begins!"; + break; + + case "doing": + string += " until the AP exam ends!"; + break; + + case "done": + string += " since the AP exam has been completed!"; + break; + } + + examElement.textContent = string; +}