-3

There is an object. There is this method that initializes the timer in another method within the same object.

initstep1() {
  var totAns = TriviaGame.corAnswered + TriviaGame.incorAnswered;
  //for (let v=0;v<TriviaGame.arrayOfSelected.length;v++){TriviaGame.arrayOfSelected.pop();}
  //for (let u=0;u<TriviaGame.arrayOfIntervals.length;u++){clearInterval(TriviaGame.arrayOfIntervals[u]);TriviaGame.arrayOfIntervals.pop();}

  $("#maincontact0").css("display", "none");
  $("#maincontact2").css("display", "none");
  $("#maincontact1").css("display", "flex");

  if (totAns != 10) {
    TriviaGame.populatePromptContent();
  } else {
    TriviaGame.initstep3();
  }
  if (TriviaGame.corAnswered == 0 && TriviaGame.incorAnswered == 0) {
    TriviaGame.giveQuestionsClickEvents();
    TriviaGame.giveAnswersClickEvents();
  }

  $("#maincontact1qr").text() == 30;
  TriviaGame.timerOnTheRight();
}

It's called timerOnTheRight...

Here it is... Never gets cleared no matter what I do.

timerOnTheRight() {
  //for (let u=0;u<TriviaGame.arrayOfIntervals.length;u++){clearInterval(TriviaGame.arrayOfIntervals[u]);TriviaGame.arrayOfIntervals.pop();}
  console.log(TriviaGame.arrayOfIntervals);
  let countDown1 = 30;
  var thisVeryTimer = setInterval(function() {
    countDown1--;
    if ($("#maincontact1qr").text() != 1) {
      $("#maincontact1qr").text(countDown1);
    }
    if ($("#maincontact1qr").text() < 11) {
      $("#maincontact1qr").css("color", "orange");
    }
    if ($("#maincontact1qr").text() < 4) {
      $("#maincontact1qr").css("color", "red");
    }
    if ($("#maincontact1qr").text() == 1) {
      TriviaGame.arrayOfCurrent[0].timespent = "Yes";
      clearInterval(thisVeryTimer);
      TriviaGame.initstep2();
    }
  }, 600);
}
adiga
  • 34,372
  • 9
  • 61
  • 83
OlegArsyonov
  • 1,251
  • 1
  • 14
  • 18
  • 3
    I'd recommend against using angry language when not necessary, it doesn't really help anything on a platform like StackOverflow, and just adds noise – Snow Mar 03 '19 at 07:00
  • I'm sorry, it just got the best of me not being able to stop the timer for like an hour... – OlegArsyonov Mar 03 '19 at 07:13
  • In timerOnTheRight you have a counter variable `countDown1` that you then never use... What is this `#maincontact1qr` and does it really hold the numeric countdown you seem to be expecting? – jBuchholz Mar 03 '19 at 07:16
  • Where do you set `$("#maincontact1qr").text()`? – Anurag Srivastava Mar 03 '19 at 07:16

1 Answers1

0

Make sure the conditional is correct, and the inside the conditional block is working when conditioning is met. You should console out thisVeryTimer to make sure it is the same interval id.

Another issue should be the scope of the variable.

clearInterval(thisVeryTimer);

Try to move the above code out of the interval block.

Kimsea Sok
  • 140
  • 2
  • 13