JSFiddle - React, Tailwind, and code Playground
HTML
<div id="mycountdown"></div>
JavaScript
var myreset = [19, 43, 00]; // at what time to reset - 19:40:00
const dupa = new Date();
let day = dupa.getDay();
// Added myCountDownDiv variable to prevent jquery from walking the DOM o every update
var myCountDownDiv = document.getElementById('mycountdown');
var mycountdown = startCountdown();
function startCountdown() {
var enddate = calculateEndDate();
return setInterval(function() {
tickTock(calculateStartDate(), enddate)
}, 1000);
}
function calculateStartDate() { //this needs to be edited if using the server time
return new Date();
}
function calculateEndDate() {
var enddate = new Date();
enddate.setHours(myreset[0]);
enddate.setMinutes(myreset[1]);
enddate.setSeconds(myreset[2]);
return enddate;
}
function tickTock(startdate, enddate) {
var diff = enddate.getTime() - startdate.getTime();
d = diff >= 0 ? diff : diff + 24 * 3600 * 1000;
var h = Math.floor(d / 3600 / 1000);
var m = Math.floor(d / 60 / 1000) - 60 * h;
var s = Math.floor(d / 1000) - 3600 * h - 60 * m;
if (day != 6 || day!=7) {
printCountdown(h, m, s);
}
}
function pluralize(word, count) {
return (count > 1) ? word + 's ' : word + ' ';
}
function printCountdown(h, m, s) {
// Updated string concatination. 'and' was deisplayed after the seconds value
var t = h + pluralize(' hour', h) + m + pluralize(' minute', m) + ' and ' + s + pluralize(' second', s);
myCountDownDiv.innerText = t;
// Removed jquery
//$('#mycountdown').html(t);
}