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

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

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!

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.