Subhodeep Mukherjee
Subhodeep Mukherjee

Reputation: 11

I dont know why clearInterval isnt working?

I dont want to change much of my code but I think here I am missing something. clearInterval isnt working somehow. check my code.

let seconds = 10;
let minutes = 0;

const myInterval = setInterval(myTimer, 250);

function myTimer() {
    seconds--;
    console.log(`Seconds ${seconds}`);
    if (seconds <= 0) {
        minutes--;
        seconds = 60;
    }
    console.log(`Minutes ${minutes}`);
   
}

if (seconds ==0 && minutes == 0) {
    clearInterval(myTimer)
}

Upvotes: 0

Views: 36

Answers (1)

IT goldman
IT goldman

Reputation: 19521

You had a few erros. Need to clearInterval on myInterval. Also, moved the condition to check end of loop inside the loop of myTimer.

let seconds = 10;
let minutes = 0;

const myInterval = setInterval(myTimer, 250);

function myTimer() {
  seconds--;

  console.log(`Minutes ${minutes} Seconds ${seconds}`);
  
  if (seconds == 0 && minutes == 0) {
    clearInterval(myInterval)
    console.log("done")
  }



  if (seconds <= 0) {
    minutes--;
    seconds = 60;
  }

}

Upvotes: 1

Related Questions