setInterval for countdown timer isn't working, was seeing if someone can help me take a look

Hello! I’m making a Javascript countdown timer and it’s almost complete, but when I try to call my countdown function with setInterval, it doesn’t seem to be initiating the countdown. I was seeing if someone can help me fix this. Thanks!

Codepen link: https://codepen.io/Grewz/pen/poKvaGm

type or paste code here

The setInterval is working fine. Add the following line of code as the first line of your remainingTime function to see why nothing appears to be changing.

console.log(difference);

So…it’s not getting it because it’s all in milliseconds?

Think about the order in which your code executes. With the way you have it now, difference gets calculated one time, so it will always be the same.

oh, gotcha. Thank you!

so this is what I have so far:

const date = new Date;
const countdownDate = new Date("February 2, 2023 1:40");
const difference = countdownDate.getTime() - date.getTime();
const second = document.getElementById("secs");
const minute = document.getElementById("mins");
const hour = document.getElementById("hours");
const days = document.getElementById("days");
console.log(difference);
function remainingTime(day, hours, minutes, seconds){
 day = Math.floor(difference * 1.1574074074074E-8);
 hours =  Math.floor((difference % 24));
 minutes = 
 seconds =  

second.innerHTML = seconds; 
minute.innerHTML = minutes;
hour.innerHTML = hours;
days.innerHTML = day;
}
remainingTime()
const value = setInterval(remainingTime, 1000);

I was able to get the days, but I’m still confused on how to get the hours, minutes, etc. because I can’t think of any other way than the one listed above. I’m unsure if there’s a formula, but yeah, I’m confused. I attempted to get difference to execute multiple times in hours but I’m not sure if that’s working because it returns a different value each time I refresh the page.

In the Codepen link in your first post, you have:

  day = Math.floor(difference / (1000 * 60 * 60 * 24));
  hours = Math.floor((difference / (1000 * 60 * 60)) % 24);
  minutes = Math.floor(difference / (1000 * 60)) % 60;
  seconds = Math.floor(difference / 1000) % 60;

This was correct.

You also have the following which calculates the number of milliseconds between two date/times:

const date = new Date;
const countdownDate = new Date("February 12, 2023 1:40");
const difference = countdownDate.getTime() - date.getTime();

This was correct.

The problem is not in the calculations themselves but WHERE you are calculating them.

1 Like

oh, where lol I’m dumb. all right, I think this did it. Thanks!