Countdown Timer with Javascript (Repeating Weekly)

Viewed 57

Here is the code I use to create a weekly repeating countdown timer. It works well, but I would like to expand on the following:

  1. How can use this code twice on a single page without conflicts? Currently, copying all elements and adjusting the "expiry" doesn't work. I would like this to display two separate outputs with different expiry dates.

  2. Output is in the following format: hours, mins, and seconds. How can it display only the most relevant information? For example, if there are 10 hours 12 minutes, and 10 seconds left, it would display: 10 hours only(excluding mins and seconds). Similarly, if there were 00 hours 00 minutes and 10 seconds left it would instead display: 10 seconds (excluding mins and hours). The same would go for minutes (excluding hours and seconds).

Any insight would be most appreciated.

Repeating Countdown Timer Javascript

<script>
var curday;
var secTime;
var ticker;
 
function getSeconds() {
 var nowDate = new Date();
 var dy = 1 ; //Sunday through Saturday, 0 to 6
 var countertime = new Date(nowDate.getFullYear(),nowDate.getMonth(),nowDate.getDate(),21,0,0); //20 out of 24 hours = 8pm
 
 var curtime = nowDate.getTime(); //current time
 var atime = countertime.getTime(); //countdown time
 var diff = parseInt((atime - curtime)/1000);
 if (diff > 0) { curday = dy - nowDate.getDay() }
 else { curday = dy - nowDate.getDay() -1 } //after countdown time
 if (curday < 0) { curday += 7; } //already after countdown time, switch to next week
 if (diff <= 0) { diff += (86400 * 7) }
 startTimer (diff);
}
 
function startTimer(secs) {
 secTime = parseInt(secs);
 ticker = setInterval("tick()",1000);
 tick(); //initial count display
}
 
function tick() {
 var secs = secTime;
 if (secs>0) {
  secTime--;
 }
 else {
  clearInterval(ticker);
  getSeconds(); //start over
 }
 
 var days = Math.floor(secs/86400);
 secs %= 86400;
 var hours= Math.floor(secs/3600);
 secs %= 3600;
 var mins = Math.floor(secs/60);
 secs %= 60;
 
 //update the time display
 document.getElementById("days").innerHTML = curday;
 document.getElementById("hours").innerHTML = ((hours < 10 ) ? "0" : "" ) + hours;
 document.getElementById("minutes").innerHTML = ( (mins < 10) ? "0" : "" ) + mins;
 document.getElementById("seconds").innerHTML = ( (secs < 10) ? "0" : "" ) + secs;
}
</script>

Call getSeconds() function to start timer with body onload

<body onload="getSeconds();">
    
    </body>

Timer Display HTML

<h6>Live in <span class="days" id="days"></span><span class="smalltext"> days,</span>
<span class="hours" id="hours"></span><span class="smalltext"> hours,</span>
<span class="minutes" id="minutes"></span><span class="smalltext"> minutes</span>
</h6>
1 Answers

You can simply do it like this

//create a javascript timer function
function timerCountDOwn(date, id) {
    const el = document.getElementById(id)

    if (!el) return
    let countDownDate = new Date(date).getTime()
    const timer = setInterval(function () {
        const now = new Date().getTime(),
            distance = countDownDate - now,
            days = Math.floor(distance / (1000 * 60 * 60 * 24)),
            hours = Math.floor((distance % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60)),
            minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60)),
            seconds = Math.floor((distance % (1000 * 60)) / 1000),
            //Format your output here
            output = `<span class="days">${days}D</span> <span class="hours">${hours}H</span> <span class="minutes">${minutes}M</span> <span class="seconds">${seconds}S</span>`

        el.innerHTML = output

        if (distance < 0) {
            clearInterval(timer)
            el.innerHTML = 'EXPIRED'
        }
    }, 1000)
}

// then you can use it on multiple element in a single page
timerCountDOwn('Jan 5, 2023 00:00:00', 'timer1')
timerCountDOwn('Aug 25, 2022 00:00:00', 'timer2')
timerCountDOwn('Oct 30, 2022 15:22:54', 'timer3')
.timer > span {
  display: inline-block;
  background: #db0808;
  color: #fff;
  padding: 5px 10px;
  border-radius: 5px;
}
<div id="timer1" class="timer">...</div>
<div id="timer2" class="timer">...</div>
<div id="timer3" class="timer">...</div>

Related