I am developing a number increase with setInterval (), and I want the setInterval to stop when the counter reaches a certain number.
var cont = 0;
var rango = document.getElementById('rango');
var id = setInterval(function(){
rango.innerHTML = cont;
cont++;
}, 1000);
if(cont == 10)
{
clearInterval(id);
}
The problem is that to carry out the conditional the variable cont must be in the same scope, but it is not since it is inside the function setInterval. What I do is take it out and put it outside.
var id = setInterval(function(){
rango.innerHTML = cont;
}, 1000);
cont++;
if(cont == 10) {
clearInterval(id);
}
but when doing this, being outside the setInterval does not increase. Who knows how to locate the code? Who explains to me? who can help me? thanks in advance