I have this javascript code
function updateClock() {
time = time + 1;
var mins = 1;
var secs = 50
var mins = parseInt(mins/ 60);
var secs = time - mins * 60;
mins = mins.toString();
secs = secs.toString();
if (mins.length < 2)
mins = "0" + mins;
if (secs.length < 2)
secs = "0" + secs;
var timeDisp = document.getElementById("timerDisp");
timeDisp.innerText = mins + ":" + secs;
}
It is counting how much time has passed, but what if I want to do a countdown? (or do the reverse) instead of keeping track of the time, I want to give if a definite time then as time goes by the time decreases, let’s say 1 minute and 15 seconds, how would I execute that on the given code?
You can execute your
updateClock()every 1 second withsetTimeout()to create a countdown.