I have a code and it seems to work normally but in minutes I want to start the counter with “0” like this but when it goes from 9 it clears the “0” again.
If I remove the “0” from that line and remove it from the html it works, but it looks really weird without a 0 before it.
minutesTens.innerHTML = “0” + minutes;
What can I improve on this stopwatch, I’m a beginner in javascript.. thanks in advance to everyone.
<div class="contador" id="time">
<span id="hors">0</span>
<span class="dots">:</span>
<span id="minutes">00</span>
<span class="dots">:</span>
<span id="seconds">00</span>
<span class="dots">.</span>
<span id="tens">00</span>
</div>
<div class="btn">
<button id="btnStart">Start</button>
<button id="btnPausar">Stop</button>
<button id="btnResetar">Reset</button>
</div>
window.onload = function () {
var tens = 0;
var seconds = 0;
var minutes = 0;
var hors = 0;
const horsTeans = document.getElementById("hors");
const minutesTens = document.getElementById("minutes");
const secondsTens = document.getElementById("tens");
const secondTens = document.getElementById("seconds");
const btnStart = this.document.getElementById("btnStart");
const btnPausar = this.document.getElementById("btnPausar");
const btnResetar = this.document.getElementById("btnResetar");
let Intervalo;
function startTimer() {
tens++;
if (tens < 9) {
seconds.innerHTML = "0" + tens;
}
if (tens > 9) {
secondsTens.innerHTML = tens;
}
if (tens > 99) {
seconds++;
secondTens.innerHTML = "0" +
seconds;
tens = 0;
secondsTens.innerHTML = "0" + tens;
}
if (seconds > 9) {
secondTens.innerHTML = + seconds;
}
if (seconds > 10) {
seconds = 0;
minutes += 1;
minutesTens.innerHTML = "0" + minutes;
}
if (minutes > 59) {
seconds = 0;
minutes = 0;
hors++;
horsTeans.innerHTML = + hors;
}
}
btnStart.onclick = function () {
clearInterval(Intervalo);
Intervalo = setInterval(startTimer, 10);
};
btnPausar.onclick = function () {
clearInterval(Intervalo);
};
btnResetar.onclick = function () {
clearInterval(Intervalo);
tens = "0";
seconds = "00";
minutes = "00";
hors = "00";
secondsTens.innerHTML = tens;
secondTens.innerHTML = seconds;
minutesTens.innerHTML = minutes;
horsTeans.innerHTML = hors;
};
};