Simple:
Let's assume you need 2 clocks, each with different duration time (if you need more than 2 clocks, or only one, just extrapolate the explanation).
- Set running and stopped clock styles:
.runningClock, stoppedClock
{width:75px;
color:white;
font-weight:bold;
border:3px double white
}
.runningClock { background:#1ABB9C; /* use your favorite green/turquoise */ }
.stoppedClock { background:red; }
- Create each clock in HTML with an ID (I'm using DIV, but it can be a TD, a SPAN or other block object) [ignore extra spaces below, typed here to make code visible]:
- < div id=clock1>< /div>
- < div id=clock2>< /div>
- within your JS block, build these 2 functions:
function Secs2Clock( numSecs ) { // converts # of seconds to a digital clock format )
return ( parseInt( numSecs /3600 ).toString().padStart(2,"0") + ':' +
(parseInt( (numSecs /60) )%60).toString().padStart(2,"0") + ':' +
parseInt( numSecs %60 ).toString().padStart(2,"0") );
}
function runClock( oneClock, hours, minutes ) {
var secs=hours*minutes*60;
oneClock.className="runningClock"; // sets it to paint green, running
var runningClock=setInterval( function() { //starts clock refreshing by second
oneClock.innerHTML = Secs2Clock( secs );
if ( secs>0 ) secs-- // single instruction if true, feel free to add curled brackets for readability
else {
oneClock.innerHTML="00:00:00";
oneClock.className="stoppedClock";
clearInterval( runningClock );
}
}, 1000
);
}
- Within your main JS block (or your "BODY onLoad" function or wherever you want the clock[s] to launch), add this line for each clock (in this example, 2 different ones); or else within the onClick action or whatever action (A HREF click, etc) where you want the specific clock(s) to start (in this example, this is part of the main JS code, to launch both clocks with different remaining times)
runClock( clock1, 1, 0 ); // will start as "01:00:00"
runClock( clock2, 6, 30 ); // will start as "06:30:00"