Once my counter reaches 00:59 (essentially 00:00), I require a day to elapse before updating to 1

I'm having trouble getting the day to increment by 1, I think my math may be incorrect.

Here is what I have and where the issue lies:

  1. The counter works perfectly (100%)
  2. I've placed the number into an "id" tag as shown below.
  3. I just can't seem to figure out the correct math needed for 24 hours for some reason.

let daysLabel = document.getElementById("days");
let minutesLabel = document.getElementById("minutes");
let hoursLabel = document.getElementById("hours");
let totalSeconds = 0;
let dayUpdate = 0;
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));
  // I believe there's an issue with my math which is why the time isn't updating.
  daysLabel.innerHTML = pad(parseInt(totalSeconds / 60 == 24));
}

function pad(val) {
  let valString = val + "";
  if (valString.length < 2) {
    return "0" + valString
  }
  return valString
}
<label id="days">Day: 1</label> <label id="hours">00</label>:<label id="minutes">00</label><br />

I tried copying the counter info in an attempt to increase the days count but failed. I cannot get the day to increment by +1 every new day based on the timer.

Day 1: 22:59 - time changes to 00:00 again and the day should increase by 1 Day 2: 00:01

My suggestions and thoughts on the issue are as follows:

The math formula might be incorrect. I think the pad() function needs to be removed. When the timer resets at 00:59 back to 0, I added code to increment the day by 1 (++days) but still no result. I'm wondering if anyone could offer a more efficient solution.

My game will run like this: Day: 1 00:00 - the counter counts 1 second every 15 seconds, allowing for more days in each round and more days in one actual day, giving users better chances as it's a round-based game with prizes.

Answer №1

The script that updates the text is attempting to calculate the day based on the total number of seconds, but it resets the total seconds count every time a new day begins. There is a variable dayUpdate that tracks this information, however, it is not being utilized effectively.

let daysLabel = document.getElementById("days");
let minutesLabel = document.getElementById("minutes");
let hoursLabel = document.getElementById("hours");
let totalSeconds = 0;
let dayUpdate = 0;
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));
  daysLabel.innerHTML = `Day: ${dayUpdate + 1}`; // +1 optional if you want day zero
}

function pad(val) {
  let valString = val + "";
  if (valString.length < 2) {
    return "0" + valString
  }
  return valString
}
<label id="days">Day: 1</label> <label id="hours">00</label>:<label id="minutes">00</label><br />

Perhaps reconsider simplifying the approach by only dealing with seconds and not maintaining a separate tracker for days. The function below takes a time value and outputs the corresponding days, hours, minutes, and seconds.

function expandSeconds(time) {   
    let period = { DAY: 86400, HOUR: 3600, MINUTE: 60 } // number of seconds in each period
    let days = parseInt(time / period.DAY,10)
    time -= days * period.DAY
    let hours = parseInt(time / period.HOUR, 10)
    time -= hours * period.HOUR
    let minutes = parseInt(time / period.MINUTE, 10)
    time -= minutes * period.MINUTE
    return { days: days, hours: hours, minutes: minutes, seconds: time }
}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What is the best way to connect the user-input data with text in a span element?

Recently, I've started learning Vue.js and I'm facing an issue with binding data from an input field to a span element. Instead of appending the data, it's showing me undefined Here is the code snippet: new Vue({ el: "#app", data: { ...

The stopImmediatePropagation method is not functioning properly as anticipated

Take a look at this code snippet : HTML <a href="http://www.google.com" target="_blank" id="myLink" class="anyclass">testlink</a> JAVASCRIPT $('#myLink').on("mousedown",doMouseDown); function doMouseDown(e) { e.stopImmediateP ...

Conduct unit testing on the React component created by a function that takes an array of objects as a parameter

Looking to test a function that returns a React component using enzyme. Component const overrideTimeoutValues = [ {minutes: 0, seconds: 10}, {minutes: 0, seconds: 15}, {minutes: 0, seconds: 20}, {minutes: 0, seconds: 30}, {minutes: 1, ...

Incorporating text into the border without increasing the spacing

I managed to create a unique CSS shape using clip paths as the border of a div. However, I am now facing an issue where I cannot add text to this border without it getting cut off due to the way the shape was created (possibly because of overflow: hidden). ...

"Incorporate a new dropdown menu and then append the selected item to an array within

I am trying to enhance my user interface by adding two new select boxes (from and until) dynamically using ng-click, but I am facing challenges in binding these selects to my array in the scope. Here is the HTML code snippet: <div ng-repeat="time in a ...

Issue with Jquery function causing page content to disappear

When using this JQuery function to generate a table based on JSON, I am facing an issue where the content that should appear after this function is not displaying properly. I have tried adding an HTML table after the function but it does not seem to work a ...

Modify the sticky menu color upon scrolling

While working on a client's website, I've stumbled upon an issue that has left me scratching my head. I'm looking to modify the color of the sticky menu once it's scrolled. Can anyone assist me in creating a custom CSS code to implemen ...

Stop React router from changing routes when the back button is pressed and also close any open pop

Within my React application, I've integrated a popup on a specific page. However, when the user opens the popup and then hits the browser's back button, they are directed to the previous page rather than simply closing the popup. I've tried ...

How to make Angular 5 wait for promises to finish executing in a for loop

My task involves working with an array like this: arr = ['res1', 'res2', 'res3']; For each value in the arr, I need to make an API call that returns a promise. arr.forEach(val => this.getPromise(val)); The getPromise me ...

Utilizing JSON to transfer information to a React component displaying an Apexchart timeline

I am trying to create a dynamic timeline using Apex Chart by loading data from a JSON file. The demo with hardcoded data can be viewed here. Here is an example of how my data.json file is structured: [{ "name": "Booked", ...

Steps to refresh a .ejs page with updated information following an ajax request

I am in need of re-rendering my homepage.ejs with new data on the server side following an ajax request. Although I am aware that you can somehow re-render the elements in the ajax callback, I am interested in finding out if it is possible to simply re-ren ...

Adding more pixels to the height of an element does not actually make it larger

Recently, I've been working with material UI and I have a scrolling list of items that I want to resize to be larger. However, I've noticed that no matter how I adjust the height in pixels, the items never exceed 140px. Below is the code snippet ...

When deploying a website with the default Hugo theme on Netlify, I encountered a strange issue where the webpage appeared blank. Sur

After coming across a question similar to mine about creating a static page using blogdown with the same Hugo theme as the main site on Stack Overflow, I still find myself struggling to understand the solution provided. As an absolute beginner, I am curren ...

Creating custom shaders for YouTube videos within a Three.js userscript

I want to add a shader effect to YouTube videos. My current approach involves using Three.js to implement a shader on a video. Specifically, I am trying to adapt this example of applying a shader to a video (code available here) into a Tampermonkey usersc ...

Modify CSS when scrolling, based on the size of the screen

I've implemented this JQuery script to modify elements in my header as the user scrolls. Is there a way to limit these modifications based on the screen size? Here's the code snippet: $(window).scroll(function() { // Once the user has scro ...

Formatting JSON data in a grid view using ReactJS

Within my React component, I am faced with the task of displaying JSON data retrieved from a 3rd Party API. The structure of the JSON is as follows: [ { color: "red", value: "#f00" }, { color: "green", value: "# ...

Output a MySQL field value into a checkbox by utilizing an if statement

Hey there, I'm really close to getting this statement to work correctly but I just can't seem to figure it out. So I have an overtime field from MySQL and I am able to display it. However, I'm trying to make it either checked or not checked ...

Javascript: Issue with loading image resource onto a specific div element

Seeking guidance on how to display the actual image resource on a div tag: Here is the script in full: var smileys = []; smileys[":)"] = "happy.png"; smileys[":D"] = "laugh.png"; smileys[":3"] = "meow.png"; smileys[":{"] = "must.png"; smileys[":V"] = ...

The elements in the list are too large for the designated area on Internet Explorer and Chrome browsers on Mac computers

I am facing an issue with my navigation menu on Internet Explorer and Chrome on MAC. The last element of the list is not fitting into the given width (which is fixed at 1000px). It works fine on Firefox, Opera, and Windows Chrome. I cannot test Safari at t ...

Save a CSV file into a list, locate a specific key within the list, and retrieve the fourth value associated with that key

After thorough revision, the question now reads as follows: Take a look at this content in database.csv: barcode,ScQty, Name , Qty ,Code 123456 , 3 ,Rothmans Blue , 40 ,RB44 234567 , 2 ,Rothmans Red , 40 ,RB30 345678 , 2 ,Rothmans Gre ...