JSFiddle - React, Tailwind, and code Playground
HTML
<body>
<div id="countdown1">01:20:54</div>
<script>new StartTimer(1,'01:20:54').myTimer();</script>
<div id="countdown2">02:30:48</div>
<script>new StartTimer(2,'02:30:48').myTimer();</script>
<div id="countdown3">03:40:32</div>
<script>new StartTimer(3,'03:40:32').myTimer();</script>
<div id="countdown4">04:50:20</div>
<script>new StartTimer(4,'04:50:20').myTimer();</script>
</body>
JavaScript
function myFormat(num){
return (num < 10)? ("0"+num): num;
}
function StartTimer(num, time){
this.parts = time.split(':');
this.hours = this.parts[0];
this.minutes = this.parts[1];
this.seconds = this.parts[2];
this.span = $('#countdown' + num);
}
StartTimer.prototype.myTimer = function(){
var s = this.seconds;
var m = this.minutes;
var h = this.hours;
var sp = this.span;
var t = setInterval(function(){
s--;
if(s == -1) {
s = 59;
m--;
if(m == -1) {
m = 59;
h--;
if(h == -1) {
alert("timer finished");
clearInterval(t);
return;
}
}
}
//console.log();
//console.log();
//console.log();
sp.text(h + ":" + myFormat(m) + ":" + myFormat(s));
}, 1000);
}