From b48736df9f12f76253e74a1cd0359644fb2a6b7d Mon Sep 17 00:00:00 2001 From: Squidly271 Date: Tue, 22 Jul 2025 03:03:12 -0400 Subject: [PATCH] Refactor: Login cooldown timer --- emhttp/plugins/dynamix/include/.login.php | 75 +++++++++++------------ 1 file changed, 37 insertions(+), 38 deletions(-) diff --git a/emhttp/plugins/dynamix/include/.login.php b/emhttp/plugins/dynamix/include/.login.php index 2cf8af4f5..8050bc4b9 100644 --- a/emhttp/plugins/dynamix/include/.login.php +++ b/emhttp/plugins/dynamix/include/.login.php @@ -616,46 +616,45 @@ $isDarkTheme = $themeHelper->isDarkTheme(); document.body.appendChild(errorElement); } - let timeInSecs; - let ticker; - - function startTimer(secs) { - timeInSecs = parseInt(secs); - ticker = setInterval(tick, 1000); - } - - function tick() { - var secs = timeInSecs; - - updateMessage(prettyTime(secs),"countdown"); - - if (secs > 0) { - timeInSecs--; - } else { - clearInterval(ticker); - // remove the error message after 10 seconds after the timer has expired - updateMessage("","error"); - setTimeout(function() { - updateMessage("","error"); - }, 10000); - return; - } - } - function updateMessage(msg,ID) { - var element = document.getElementById(ID); - if (element) { - element.innerHTML = msg; - } - } - function prettyTime(secs) { - var mins = Math.floor(secs/60); - secs %= 60; - return ( (mins < 10) ? "0" : "" ) + mins + ":" + ( (secs < 10) ? "0" : "" ) + secs; - } - if ( document.getElementById("countdown") ) { - startTimer(); + if ( document.getElementById("countdown") ) { + let timeInSec, ticker, startTime, endTime; + + startTimer(); + + function startTimer(secs) { + startTime = Math.floor(Date.now() / 1000); + endTime = startTime + parseInt(secs); + ticker = setInterval(tick, 1000); } + + function tick() { + var secs = endTime - Math.floor(Date.now() / 1000); + + if (secs <= 0) { + clearInterval(ticker); + // remove the error message after 10 seconds after the timer has expired + updateMessage("","error"); + setTimeout(function() { + updateMessage("","error"); + }, 10000); + return; + } + updateMessage(prettyTime(secs),"countdown"); + } + + function updateMessage(msg,ID) { + var element = document.getElementById(ID); + if (element) { + element.innerHTML = msg; + } + } + function prettyTime(secs) { + var mins = Math.floor(secs/60); + secs %= 60; + return ( (mins < 10) ? "0" : "" ) + mins + ":" + ( (secs < 10) ? "0" : "" ) + secs; + } + }