Simple Timer Program I have built a timer program in JavaScript. It decrements one counter every second, and if that counter is at zero, it decrements another counter, and sets the first counter to 59. If the second counter now is below zero, then it stops the timer. Would it be more efficient to store the total seconds left, and calculate the minutes on the fly? Is there other stuff I can improve on?



var timerRun; // Variable for holding the interval
          var timeLeft; //Array; index 0 keeps track of the minutes, and index 1 keeps track of the seconds
          var timerRunning = false; //Boolean to keep track of whether the timer is running
          function tick() {
              // Subtract 1 second
              timeLeft[1] -= 1;
              if (timeLeft[1] &lt; 0) { // Decrement the minute if neccessary
                  timeLeft[0] -= 1;
                  timeLeft[1] = 59; 
              }
              if (timeLeft[0] &lt; 0) { // Check if the timer is done
                  clearInterval(timerRun);
                  alert("Timer is done!");
                  timerRunning = false;
                  document.getElementById("timer").innerHTML = "Simple Timer";
              }
              if (timeLeft[1] &lt; 10) { // Display the seconds as a two-digit number
                  timeLeft[1] = "0" + timeLeft[1];
              }
              document.getElementById("timer").innerHTML = timeLeft.join(":");
              if (timerRunning == false) {
                  document.getElementById("timer").innerHTML = "Simple Timer";
              }
          }
          function time() { // Restart timer
                clearInterval(timerRun)
                timerRun = setInterval(tick, 1000);
                // Create array
                timeLeft = document.getElementById("time").value.split(":");
                timerRunning = true;
          }
&lt;!DOCTYPE html&gt;
&lt;html&gt;
  &lt;head&gt;
  &lt;/head&gt;
  &lt;body&gt;
      &lt;h1 id="timer"&gt;Simple Timer&lt;/h1&gt;
      &lt;input type="text" id="time" value="15:00"&gt;
      &lt;button type="button"  onclick="time();" id="start"&gt;Start&lt;/button&gt;

  &lt;/body&gt;
&lt;/html&gt;
You are basically trying to show timer, which can be up to hours. Best way to calculate and represent that for your usecase is definitely by just one variable, which holds all the seconds. That way your code handling current state is a lot simpler - you have just one variable, you decrease it every second until it's done.
You will have more work displaying that value as you will have to calculate minutes and seconds, but that's fine.
Definitely extract that to function - you pass number of seconds and you get string representing it. You may even consider using library for that or a number formatter. There are plenty ways how to do that:
https://stackoverflow.com/questions/3733227/javascript-seconds-to-minutes-and-seconds
Your variable timerRunning is redundant. In your code same meaning has expression timeLeft[1] &lt; 0 &amp;&amp; timeLeft[0] &lt; 0. You really don't want redundancy, that leads to bugs. Again you can extract to function isTimerRunning if you need it. But again if you end up with only one timeLeft variable with representing all in seconds, you just need !timeLeft to check, if timer is over.
Probably not a problem in your case, but keep in mind, that setInterval is not very reliable time-wise. It pings "around" every second, but it jumps a lot. More on this here:
https://stackoverflow.com/questions/29971898/how-to-create-an-accurate-timer-in-javascript


Edit:


In this line function time() { // Restart timer you poorly name your function and then you have to comment it to describe what it does. Why not just name function restartTimer and you can delete comment?