I have a setInterval which is supposed to alert the amount of incremented a
every 1 second only under a condition which is if a <= 5
, else it should make return a = 1
, which means it should turn a
back to 1 and stop the setInterval from executing as initialized, but surprisingly it starts to execute the setInterval
over and over again, each round with a = 1
incrementng.
I want the setInterval to be executed only if a <= 5
and as soon as this condition isn't true, then stop the setInterval
and turn a back to 1, I don't want it to start the functoin over and over again, though it is turning a
back to 1.
How can I make this happen?
function doSomething() {
let a = 1;
foo = setInterval(() => {
if (a <= 5) {
a++;
console.log(a)
} else {
return a = 1;
}
}, 1000)
return 0;
}
setTimeout(doSomething, 5000);