0

I am trying to get my countdown to stop at zero however it resets rather than stops.

I've added a conditional statement at the end of the runTimer function but nothing happens. It just resets.

I'm going off of an exercise where it counts up. I'm modifying it a bit and having it countdown.

function runTimer() {
    let currentTime = leadingZero(timer[1]) + ":" + leadingZero(timer[2]);
    theTimer.innerHTML = currentTime;
    timer[3]--;

    timer[0] = Math.floor((timer[3]/100)/60); //minutes
    timer[1] = Math.floor((timer[3]/100) - (timer[0] * 60)); //seconds
    timer[2] = Math.floor(timer[3] - (timer[1] * 100) - (timer[0] * 6000)); //hundredths

    if (currentTime = 0) {
      clearInterval(interval);
    }

}

I expected it to stop at zero but it just resets back to 59:00... and I want it to stop at 00:00.

Justin
  • 133
  • 5
  • 4
    Then why use an interval? Also, `=` and `==` (preferably `===`) are different things. – Dave Newton Apr 23 '19 at 15:08
  • 4
    you are setting current time, not comparing it, also you compare a string against an int – Pete Apr 23 '19 at 15:08
  • `currentTime` looks like formatted output, and thus shouldn't be used for internal comparisons. It looks like what you want is `if (timer[3] == 0)` –  Apr 23 '19 at 15:09

2 Answers2

2

The problem is this part:

if (currentTime = 0)

Since you're checking if the value is 0, you don't want to assign a value of 0, instead you want to compare currentTime with 0. This is done with the === operator. So to summarize:

= is to assign a value to a variable. ( left is variable and right is the assignment)

== or === is to compare the two values.(Difference between == and === in JavaScript)

Your line should be:

if (currentTime == 0)

Hope it helped. :)

Jérémy
  • 223
  • 2
  • 12
1

Two points.

1) As already mentioned, your if clause will not work because you are using "=" (a single equal sign). A single equal sign in JavaScript does assign values, not compare values. You however want to compare values and need to use double or triple equals.

2) Even if you change that, currentTime will probably never evaluate to zero, since you have assigned a string to currentTime before. So even if currentTime is "00:00", the string will not evaluate to 0 (see image)

enter image description here

I guess you more want to do something like this:

if (timer[2] === 0 && timer [1] === 0 && timer[0] === 0) {
    clearInterval(interval);
}

Or most probably this will suffice:

if (timer[3] <= 0) {
    clearInterval(interval);
}
sborn
  • 111
  • 1
  • 9
  • Cool. I was able to stop the time however I'm now at the point where the timer stops at 00:01. Not sure how to override that "00:01". – Justin Apr 30 '19 at 15:05
  • Yes, makes sense. Reason is you're decreasing timer[3] *before* you do the check of `currentTime`. Best thing to do is moving the line `timer[3]--;` *after* `if (timer[3] <= 0) {...}` So that if *first* checks if the timer is at zero and - if timer is not at zero - decreases the timer. This way you should actually get to see "00:00". – sborn May 02 '19 at 09:50