summaryrefslogtreecommitdiff
path: root/app/static/custom.js
diff options
context:
space:
mode:
authorKaran Jayachandra <mail@karanjayachandra.com>2025-07-06 13:33:26 +0200
committerKaran Jayachandra <mail@karanjayachandra.com>2025-07-06 13:33:26 +0200
commit279faac0323da94535e6ee3b82458f858ec7178a (patch)
tree992846795ff24cdb019ab63bcc3a332cbbba3300 /app/static/custom.js
parentc773faeea93fe5db2eb493c79c0c82f46336493c (diff)
Working version
Diffstat (limited to 'app/static/custom.js')
-rw-r--r--app/static/custom.js87
1 files changed, 87 insertions, 0 deletions
diff --git a/app/static/custom.js b/app/static/custom.js
new file mode 100644
index 0000000..2e4b049
--- /dev/null
+++ b/app/static/custom.js
@@ -0,0 +1,87 @@
+let minsPerRound = Number(sessionStorage.getItem("minsPerRound")) || 12;
+let timerRunningFlag = Number(sessionStorage.getItem("timerRunningFlag")) || 0;
+let deadline = Number(sessionStorage.getItem("deadline")) || new Date().getTime();
+let clock = 0;
+
+function setTimer(m, s) {
+ let timer = document.getElementById("timer-display");
+ m = m < 10 ? ("0" + m) : m;
+ s = s < 10 ? ("0" + s) : s;
+ timer.value = `${m}:${s}`;
+}
+
+function setSessionData() {
+ sessionStorage.setItem("deadline", deadline);
+ sessionStorage.setItem("minsPerRound", minsPerRound);
+ sessionStorage.setItem("timerRunningFlag", timerRunningFlag);
+}
+
+function resetTimer() {
+ clearInterval(clock);
+ setTimer(minsPerRound, 0);
+ timerRunningFlag = 0;
+ setSessionData();
+}
+
+function updateTimer(milliseconds) {
+ const m = Math.floor((milliseconds % (1000 * 60 * 60)) / (1000 * 60));
+ const s = Math.floor((milliseconds % (1000 * 60)) / 1000);
+ setTimer(m, s);
+}
+
+function refreshTimer() {
+ const now = new Date().getTime();
+ const millisecondsToDeadline = deadline - now;
+ if (millisecondsToDeadline > 0) {
+ updateTimer(millisecondsToDeadline);
+ return;
+ }
+ resetTimer();
+ const notification = new Notyf({
+ duration: 30 * 1000,
+ position: {x: 'center', y: 'top'},
+ types: [{
+ type: 'success',
+ background: 'indianred',
+ duration: 1000 * 1000,
+ dismissible: true
+ }]
+ });
+ notification.success("Round completed");
+}
+
+function startTimer(fresh) {
+ if (fresh) {
+ now = new Date().getTime();
+ const millisecondsToDeadline = minsPerRound * 60 * 1000;
+ deadline = now + millisecondsToDeadline;
+ }
+ timerRunningFlag = 1;
+ setSessionData();
+ clock = setInterval(refreshTimer, 1000);
+}
+
+if (timerRunningFlag) startTimer(0); else resetTimer();
+
+document.getElementById("timer-start").addEventListener('click', ()=> {
+ if (timerRunningFlag != 0) return;
+ startTimer(1);
+});
+
+document.getElementById("timer-stop").addEventListener('click', ()=>{
+ resetTimer();
+});
+
+document.getElementById("timer-inc").addEventListener('click', ()=> {
+ if (timerRunningFlag == 0) {
+ minsPerRound += 1;
+ resetTimer();
+ }
+});
+
+document.getElementById("timer-dec").addEventListener('click', ()=> {
+ if (timerRunningFlag == 0 && minsPerRound != 0) {
+ minsPerRound -= 1;
+ resetTimer();
+ }
+}); \ No newline at end of file