-2

I am creating a game that when the play button is clicked, the timer starts. When my time is 0 I want to the paragraph with #timeline to be replaced with different text, which works perfectly. However how can I simultaneously add a .class to the replacement text so I can style "Your time is up!" differently when it is displayed.

    var secondsLeft = 20;

    function startTimer(){
      setInterval(myTimer, 1000);
    }

    function myTimer(){
      if(secondsLeft!=0){
        document.getElementById("time").innerHTML = secondsLeft-=1;
      } else {
        document.getElementById("timeLine").innerHTML = "Your time is up!"; 
      }
    }
Jonathan Lonowski
  • 121,453
  • 34
  • 200
  • 199
  • 6
    No, you can not _“have two Else conditions run simultaneously”_ … but you can of course put _more than one_ statement into the else _block_. – CBroe Nov 14 '15 at 00:26
  • 2
    Something like `if(cond) { whatever; } else { firstElse; secondElse; }`? – Oriol Nov 14 '15 at 00:27
  • This is one of the funniest questions I've seen in sooooo long. Thanks for the laugh! – Luke Joshua Park Nov 14 '15 at 00:28
  • Are you just asking how to set a `class` with JavaScript? A simple Google search can be pretty helpful for that: http://stackoverflow.com/questions/195951/change-an-elements-class-with-javascript – David Nov 14 '15 at 00:35
  • @Oriol exactly want I meant and it worked perfectly - thanks – jdmiragliotta Nov 14 '15 at 00:42
  • This will require a quantum computer. See https://en.wikipedia.org/wiki/Quantum_computing. –  Nov 14 '15 at 01:55

1 Answers1

0

No. You cannot. With any programming language, you need to follow the basics of an if statement. If statements can have numerous else conditions, but only one condition will be met. This is how control flow is accomplished. It will keep going down the list until one condition is met. In your case, the first condition with "time" is the proper code that would be run.

As far as styling goes for time === 0, you should try putting both in the same else condition and see if that is what you are looking for.

Mark A
  • 1,995
  • 4
  • 18
  • 26