JSFiddle - React, Tailwind, and code Playground
HTML
<div id="chrono1"></div>
<div id="chrono2"></div>
<div id="chrono3"></div>
JavaScript
var chrono = function(start, callback) {
var _this = this;
this.start = start;
this.current = start;
var calc = function() {
var text = '';
if(_this.current>0) {
var d = new Date(0,0,0,0,0,_this.current),
h=d.getHours(),
m=d.getMinutes(),
s=d.getSeconds();
text = 'Il reste ' + (h>0 ? h + ' h ':'') + (m>0 ? m+' min ':' ') + (s>0 ?+s+' s ':' ');
_this.current--;
} else {
clearInterval(_this.timer);
text = 'Le temps est écoulé';
}
if(typeof callback == 'string') {
document.getElementById(callback).innerHTML = text;
} else {
callback(text);
}
}
this.timer = setInterval(calc, 1000);
};
new chrono(4346, 'chrono1');
new chrono(6587, 'chrono2');
new chrono(56, 'chrono3');
new chrono(132, function(text) {
console.log(text);
});