-1
So I’m beginner and wanted a tip how to do so when click the button do not stay 2 chronometer at the same time one on top of the other believe that with clearInterval solve more I got no idea where to put it
function startTimer(duration, display) {
var timer= duration, min, sec
setInterval(function () {
min = parseInt(timer / 60, 10);
sec = parseInt(timer % 60, 10);
min= min<10 ? '0' + min :min;
sec= sec<10 ? '0' + sec :sec;
display.textContent = min + ':' + sec;
if (--timer<0) {
timer = duration;
}
}, 1000);
}
function iniciarContagem() {
var fourmin = 60* 4; //conversão para segundos
display = document.querySelector('#timer'); //elemento para exibir o timer no document html
startTimer(fourmin, display); //inicia as functions ou chama as functions
document.getElementById("btn").onclick = iniciarContagem;
}
<div id="timer"></div>
<input id="btn" type="button"
value="Iniciar Contagem"
onclick="iniciarContagem()">
there was only one problem that when it reaches zero the chronometer continues with negative numbers
– Brenekat our brenoob
I put a condition in the Trim function after saying the display content.textContent = min + ':' + sec; if (min <= 00 && sec<=00) {//condition to stop the display clearInterval(timerAtual); } ready was great worth the force
– Brenekat our brenoob
"there was only one problem that when it reaches zero...." but this is already another question :) the problem of not restarting the chronometer is solved in the answer, if it was useful do not forget to vote
– Ricardo Pontual