-1

I have to clear the interval but I don't know why it isn't working. Normally it schould be quite simple, but I just can't find out whats wrong.

if(data.groupStandings[0].active === true){
  Tabelle();
  var timerId = setInterval(countdown, 1000);
  if(data.groupStandings[0].active === false){
    clearInterval(timerId);
  }
  var timeLeft = 5;       
  function countdown() {
    if (timeLeft < 0) {
      clearTimeout(timerId);
      code();
      timeLeft--;
      if(timeLeft <= -5){
        timeLeft = 5;
      }
    } else {
      code();
      timeLeft--;
    }
  }
}
rrk
  • 15,677
  • 4
  • 29
  • 45
  • Why are you trying to clear the interval *immediately* after setting it? Why not just not set it in the first place? Additionally, under what conditions do you ever expect `data.groupStandings[0].active === true` and `data.groupStandings[0].active === false` to *both* be true? – David Aug 30 '21 at 20:42
  • 1
    You only enter this block if `active === true` and only cancel the interval if `active === false`? – ray Aug 30 '21 at 20:42
  • `clearTimeout` should be `clearInterval`. – Barmar Aug 30 '21 at 20:44

1 Answers1

0

You're entering the case when

data.groupStandings[0].active === true

but trying to clear the interval on the value being false

var timerId = setInterval(countdown, 1000);

if (data.groupStandings[0].active === false) { // here
    clearInterval(timerId);
}

so content of the if block will not be read at all unless data.groupStandings[0].active somehow changes it's state (which is not seen in the code)

Aziza Kasenova
  • 1,501
  • 2
  • 10
  • 22