I am trying to make a countdown timer that will show only from 8am till 16 and then show a message for 16hours and repeat the same loop every day. I have this code but I don't really get what is wrong with it. Any recommendations?
Appreciate. Thank you!
let getTom = () => {
var tom = new Date();
tom.setDate(now.getDate() + (1 - now.getDay() % 1));
tom.setHours(16, 30, 0, 0);
return tom; }
window.onload = function() {
var countDownDate = getTom();
let startCountdown = function() {
// Countdown Timer
var x = setInterval(function() {
var now = new Date().getTime();
var distance = countDownDate - now;
var days = Math.floor(distance / (1000 * 60 * 60 * 24));
var hours = Math.floor(
(distance % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60)
);
var minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60));
var seconds = Math.floor((distance % (1000 * 60)) / 1000);
var time =
days + " days, " + hours + "h, " + minutes + "m, " + seconds + "s ";
document.getElementById("time").innerText = time;
if (distance < 0) {
clearInterval(x);
document.getElementById("time").innerText = "test";
setTimeout(() => {
startCountdown();
// set to 16h later
countDownDate = countDownDate.getTime() + (1000 * 60 * 60 * 16);
}, (1000 * 60)) // 1m test
}
});
startCountdown();}}