I am trying to create a stopwatch in JavaScript , this works correctly, but I need it to add the minutes starting from 5, I have a counter initialized to 5 and another one that controls the minutes, but I need it to establish a span with minutes equal to 5 when the second counter is equal to the first and from there continue counting minutes...
I hope I have explained myself clearly. I attach my current code, since I can't get it to add the minutes after 5 min.
counters
var contador_segundos = 0;
var contador_minutos = 0;
var contador = 5;
var cronometro;
var tiempoContratado;
function(){
if(contador_segundos == 60){
contador_segundos = 0;
contador_minutos++;
contador++;
if(contador == contador_minutos){
minutos.innerHTML = contador;
minutos.innerHTML = contador_minutos;
}
if(contador_minutos == 60){
contador_minutos = 0;
}
}
segundos.innerHTML = contador_segundos;
contador_segundos++;
},1000);
UPDATE
cronometro = setInterval(function (){
if(cronometro >= startMin){
sec++;
if(sec == 60){
min++;
sec = 0;
if(min == 60){
hour++;
min = 0;
}
}
}
console.log(sec);
minutos.innerHTML = min
}, 1000);
Thanks in advance.
The problem I see is that you are increasing the counter++ and counter_minutes++ variables, therefore, they will never be the same :)
Aren't you using interval?
I did the test and your code works fine for me making the modifications that I mentioned
Look this is what I did:
The first thing I have directly called an anonymous function giving it an interval of 1 second, in that second it verifies that the initial timer variable is greater than or equal to that of 5 minutes, if not, it increases until it is fulfilled. Once this condition is met, it is nothing more than starting to increase and count mins, secs and hours, even in this case you did not ask for it but there you have it :-). Hope this can help you