JSFiddle - React, Tailwind, and code Playground
by Abhishek Kumar
HTML
<div id="display"></div>
<div id="console"></div>
JavaScript
var currentEpoch = Math.floor(new Date().getTime()/1000);
var futureEpoch = currentEpoch + (70);
var render = function () {
var currEpoch = Math.floor(new Date().getTime()/1000);
var diff = futureEpoch - currEpoch;
var t = {
h: Math.floor(diff / 3600),
m: Math.floor(diff / 60) % 60,
s: diff % 60
};
$('#console').html(JSON.stringify(t));
if (t != undefined ) {
var d = [
(t.h === 0) ? '' : t.h + 'h',
(t.m === 0) ? '' : t.m + 'm',
(t.s === 0 || t.h > 0 || t.m > 0) ? '' : t.s + 's'
];
var o = 'Countdown timer: ' + d.join(' ');
}
$('#display').html(o);
if(diff > 0) {
this.timer = window.setTimeout(render, 1000);
} else {
window.clearTimer();
}
}
render();