mercredi 12 octobre 2016

if condition lets value slip through

In javascript I have made a timer and when started I keep the value if the timer is started or not in a cookie . At the beginning of the file I assign that cookie to a variable "timerStarted". Now when I am loading another page it should check if the timer was started, if so start the timer so the timer keeps running after page reload. If the value of timerStarted is false it shouldn't start the timer. I tried the following things:

alert(timerStarted);
timerStarted = false;

if(timerStarted !== false || timerStarted !== "false" || timerStarted.valueOf() !== false)
{
    alert(timerStarted);
    startTimer();
}

At the start of my JS file:

var timerStarted = Cookies.get('timerStarted') ? Cookies.get('timerStarted') : false;

The problem is that if timerStarted is false it still runs the if condition. When I alert beforehand it is false, in the condition it is also false when the code: timerStarted = false; is not inserted. The timerStarted = false; was my last try to make sure it really had the false value, still it runs the if code. When I try the conditions with timerStarted === true, it does the other way around, the timer stops even though timerStarted is true.

Aucun commentaire:

Enregistrer un commentaire