MichaelJackson69
New Coder
I want to understand how this timer function works, please can someone help me?
My goal is to set this timer to end: 2021.11.24 12:00 (UTC)
My goal is to set this timer to end: 2021.11.24 12:00 (UTC)
JavaScript:
<script>
(function() {
var start = new Date;
start.setHours(0, 23.5, 0); // 11pm
function pad(num) {
return ("0" + parseInt(num)).substr(-2);
}
function tick() {
var now = new Date;
if (now > start) { // too late, go to tomorrow
start.setDate(start.getDate() + 1);
}
var remain = ((start - now) / 1000);
var hh = pad((0) % 60);
var mm = pad((remain / 60) % 60);
var ss = pad(remain % 60);
document.getElementById('time_hours').innerHTML = hh;
document.getElementById('time_minutes').innerHTML = mm;
document.getElementById('time_seconds').innerHTML = ss;
setTimeout(tick, 1000);
}
document.addEventListener('DOMContentLoaded', tick);
})();
</script>