Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

161
Views
How do I change the HTML DOM with Javascript when the Date changes/Time is met?

Example: When it is Midnight (hr)0 : (min) 0 : (sec) 1 Change the HTML P element to "New Day" ...

This code works but requires me to refresh the page when the time is met. I use var.getMinutes() for testing purposes.

const d = new Date();
const h = d.getHours();
const m = d.getMinutes();
const s = d.getSeconds();
const day = document.querySelector('#current-day');

function addDay() {
  if(m === 13) {
    day.innerHTML = 'New Day';
    console.log('works');
  } return
} 

window.setInterval(addDay(),1000);
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

There are two problems in your code.

  1. setInterval accepts a function as the first argument, but you're giving it the return value of the function call. You should instead pass the function to setInterval, like so: window.setInterval(addDay, 1000);.

Function call (when you execute a function):

addDay()

Function definition (when you describe what the function does):

function(args) {
    // perform some calculations using the arguments
}
  1. Inside the function addDay, you are using m. But m will be evaluated only once. It won't update as time passes (as it should). You should evaluate m inside the function. More importantly, you should evaluate the current date in the function. So every time the function executes, the value for d and m will update. (see the code snippet below)

const day = document.querySelector('#current-day');

function addDay() {
  const d = new Date();
  const h = d.getHours();
  const m = d.getMinutes();
  const s = d.getSeconds();

  if (m === 13) {
    day.innerHTML = 'New Day';
    console.log('works');
  }
}

window.setInterval(addDay, 1000);
<div id="current-day"></div>

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!