I have 2 timers I want to run seperately. 'timer'is linked to the first function 'timePassed' and should count up until the vital stats of my creature go down to 0 then it should stop. It does. However, my second timer, 'timer2' is linked to the second function 'restart'. It isn't stopping when the stats reach 0, which is want I want. Just like the first.
I suspect it might have to do with my running of 'timePassed'(function 1) within function 2 'restart'. I feel the timer runs too fast for 1 second intervals so that's another clue. But I'm not sure as I'm new to all this. Basically, how can I stop 'timer2' as soon as the stats reach 0?
//first function that works fine
function timePassed(){
message();
screen();
i++;
answer();
decrease();
changeStar();
changeDonut();
changeHeart();
if (health === 0 || happiness === 0 || hunger === 0){
dodo();
clearInterval(timer);
}
}var timer= setInterval(timePassed, 1000);
//second function whose timer isn't working correctly
function restart() {
i=0;
message();
health = 4;
happiness = 4;
hunger = 4;
screen();
timePassed();
if (health === 0 || happiness === 0 || hunger === 0){
dodo();
clearInterval(timer2);
}
} var timer2 = setInterval(timePassed, 1000);
I've tried assigning var timer2 to both timePassed() (as shown) and restart(). If I use restart(), my counter bounces up and down never decreasing enough to trigger the clearInterval.
If I remove the 'if' statement from restart(), and/or timer2, restart() just sits and doesn't count down.