Update script.js for fixes

Bug Fixes:
- Fixed bug in which reset button did not work when Timer has ended. 
- Fixed bug in which timer could end while being paused.

Added:
- Happy and Random Hour now deactivates when Timer reset.
This commit is contained in:
Lea
2023-03-27 13:36:36 +02:00
committed by GitHub
parent 40651c096e
commit 54d0ac91da
+15 -15
View File
@@ -6,6 +6,8 @@ var initialSeconds;
var paused = false;
var happy_hour_active = false;
var random_hour_active = false;
let countdownEnded = false;
let countdownUpdater = null;
var initialHoursLocal = window.localStorage.getItem('initialHours')
if (initialHoursLocal !== null) {
@@ -31,6 +33,10 @@ if(initialSecondsLocal !== null) {
resetBtn.addEventListener("click", function(){
if (happy_hour_active) specialHourHandler('Happy');
if (random_hour_active) specialHourHandler('Random');
countdownEnded = false;
initialHours = initialHoursConfig;
initialMinutes = initialMinutesConfig;
initialSeconds = initialSecondsConfig;
@@ -48,7 +54,6 @@ resetBtn.addEventListener("click", function(){
logMessage("Core", "Timer Reset.");
});
startBtn.addEventListener("click", function(){
if (paused){
@@ -86,14 +91,18 @@ startBtn.addEventListener("click", function(){
paused = false;
});
countdownUpdater = setInterval(() => {
getNextTime();
}, 1);
});
Mousetrap.bind(pauseShort, function(e) {
paused = true;
logMessage("Core", "Timer was paused");
document.getElementById("startPage").style.visibility = "visible";
document.getElementById("container").style.visibility = "hidden";
clearInterval(countdownUpdater);
});
Mousetrap.bind(happyHourShort, async function(e){
@@ -147,12 +156,9 @@ async function specialHourFunc(type){
document.getElementById("SpecialHourText").style.opacity = "0";
}
let countdownEnded = false;
let users = [];
let time;
let endingTime = new Date(Date.now());
endingTime = timeFunc.addHours(endingTime, initialHours);
endingTime = timeFunc.addMinutes(endingTime, initialMinutes);
@@ -176,6 +182,7 @@ const getNextTime = () => {
clearInterval(countdownUpdater);
countdownEnded = true;
time = "00:00:00";
logMessage("Core", "Timer Ended");
}
if (!paused){
window.localStorage.setItem('initialHours', timeFunc.getHours(differenceTime));
@@ -195,10 +202,6 @@ const getNextTime = () => {
};
let countdownUpdater = setInterval(() => {
getNextTime();
}, 1);
function randomHappy(){
if (!happy_hour_active){
if ((getRandomInt(0,10000) == 127)){
@@ -255,12 +258,9 @@ const addTimeInternal = async () => {
firstSub = true;
let addedTime = document.createElement("p");
if(happy_hour_active) {
addedTime.classList = "gold";
}
else {
addedTime.classList = "addedTime";
}
happy_hour_active ? addedTime.classList = "gold" : addedTime.classList = "addedTime";
addedTime.innerText = `+${s}s`;
document.body.appendChild(addedTime);
addedTime.style.display = "block";