SwiftGameR Posted February 16, 2023 Posted February 16, 2023 Hey all been a while since i have posted anything on here but have been working on a personal project for my game on GL Engine, I am going to be using it as a turn based game please see below for more info. Day 01 - 00:00 Every day the clock advances 1 minute every 14-15 seconds the timer will move up 1 minute to look like 00:01 until it hits 24:59 once it resets back at 00:00 Day 1 changes to Day 2 I have this working increasing the 00:00 by 01 but problem is i cant get it to stop and reset at 23:59 back to day 2 00:00 code below. <script> var minutesLabel = document.getElementById("minutes"); var hoursLabel = document.getElementById("hours"); var totalSeconds = 0; setInterval(setTime, 10); // set to 10 to make it into a timer to test if stop at 24:59 and reset to 00:00 function setTime() { ++totalSeconds; minutesLabel.innerHTML = pad(parseInt(totalSeconds % 60)); hoursLabel.innerHTML = pad(parseInt(totalSeconds / 60)); } function pad(val) { var valString = val + ""; if (valString.length < 2) { return "0" + valString; } else { return valString; } } </script> and the html is just to labels with 00 <label id="minutes">00</label>:<label id="seconds">00</label> Am i safe to assume that if i change the setintval function to 14000 which if my coding aint to rusty then that should increase the labels :01 every 14 seconds (confirmed by testing) any help would be appreciated thanks Steven (SmartWeb Productions) Quote
gamble Posted February 16, 2023 Posted February 16, 2023 (edited) So if I'm reading this right (I'm on mobile so might not be) Can't you just add an if after the ++totalSeconds line? So add if(totalSecond/60 == 24){//new day 🙂 totalSeconds= 0; } Edited February 16, 2023 by gamble Quote
SwiftGameR Posted February 17, 2023 Author Posted February 17, 2023 21 hours ago, gamble said: So if I'm reading this right (I'm on mobile so might not be) Can't you just add an if after the ++totalSeconds line? So add if(totalSecond/60 == 24){//new day 🙂 totalSeconds= 0; } I did try add a if statement but with no luck im in no rush to get it working from what i read and learning i know it can be done being honest i would proberly be easier using the date H:HH which formats the day into 24 hour format but can i manipulate it to do what i need i dunno. i will do more testing with some if statements like you said i know im close lol thanks for replyin due to the pad function its counting the length of the string and only reseting when its greater than 2 characters long but i need it to stop at 23:59 but yeah ill keep at it trial and error Quote
gamble Posted February 17, 2023 Posted February 17, 2023 45 minutes ago, SwiftGameR said: I did try add a if statement but with no luck im in no rush to get it working from what i read and learning i know it can be done being honest i would proberly be easier using the date H:HH which formats the day into 24 hour format but can i manipulate it to do what i need i dunno. i will do more testing with some if statements like you said i know im close lol thanks for replyin due to the pad function its counting the length of the string and only reseting when its greater than 2 characters long but i need it to stop at 23:59 but yeah ill keep at it trial and error I'm not sure why it's not working...he's a codepen that shows my suggested fix does work: https://codepen.io/gambleDev/pen/MWqwRJY 1 Quote
SwiftGameR Posted February 26, 2023 Author Posted February 26, 2023 (edited) On 2/17/2023 at 9:22 PM, gamble said: I'm not sure why it's not working...he's a codepen that shows my suggested fix does work: https://codepen.io/gambleDev/pen/MWqwRJY Weird i must have wrote my if function wrong thanks so much gamble exactly works how i needed 🙂 @gamble Hey sorry to bother you after you already fixed my issue i was hoping to apply simliar logic to the code so the days would update by 1 but stupid me got the maths wrong quicker posting code lol <label id="days">Day: 1</label> <label id="hours">00</label>:<label id="minutes">00</label><br /> <script> let daysLabel = document.getElementById("days"); let minutesLabel = document.getElementById("minutes"); let hoursLabel = document.getElementById("hours"); let totalSeconds = 0; let dayUpdate = 1; setInterval(setTime, 10); function setTime() { ++totalSeconds; if (totalSeconds / 60 == 24) { ++dayUpdate; totalSeconds = 0; } minutesLabel.innerHTML = pad(parseInt(totalSeconds % 60)); hoursLabel.innerHTML = pad(parseInt(totalSeconds / 60)); // {issue is here}daysLabel.innerHTML = pad(parseInt(totalSeconds * 3600 * 24)); } function pad(val) { let valString = val + ""; if (valString.length < 2) { return "0" + valString } return valString } Edited February 26, 2023 by SwiftGameR added html tags into js :/ 1 Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.