title: Javascript Timer Pause & Resume
body: <p>The code is below. </p>
  
  <p>How to add pause function and stop the timer, also set a button to pause the timer and resume when pressing resume?</p>
  
  <p>//startTimer will start the timer onload of body.</p>
  
  <pre><code> function startTimer() {
  
      userInput = "35";
  
      if(userInput.length == 0){
          alert("Please enter a value");
      } 
      else {
          var numericExpression = /^[0-9]+$/;
          if(!userInput.match(numericExpression)){
          alert("Please enter a number")
          }       
          else {
          function display( notifier, str ) {
          document.getElementById(notifier).innerHTML = str;
              }
  
          function toMinuteAndSecond( x ) {
           return Math.floor(x/60) + ":" + x%60;
    }
  
    function setTimer( remain, actions ) 
    {
  
      (function countdown() 
          {
         display("countdown", toMinuteAndSecond(remain));         
         actions[remain] &amp;&amp; actions[remain]();
         (remain -= 1) &gt;= 0 &amp;&amp; setTimeout(arguments.callee, 1000);
          })();
    }
  
  
    setTimer(userInput, {
      35: function () { display("score", "6"); document.getElementById("total").value = 6 },
      20: function () { display("score", "4"); document.getElementById("total").value = 4 },
      15: function () { display("score", "3"); document.getElementById("total").value = 3 },
      10: function () { display("score", "2"); display("notifier", "10 seconds left"); document.getElementById("total").value = 2 },
       5: function () { display("score", "1"); display("notifier", "5 seconds left"); document.getElementById("total").value = 1 },
       0: function () { display("score", "0"); display("notifier", "Time is up"); document.getElementById("total").value = 0 },
    }); 
  }  
  }
  }
  </code></pre>
  
  <p>// setTimer sets the countdown and displays the time on the page. </p>
  
  <p>//The setTimer(userInput) is to display score randomly on timer scope, as timer changes the score also changes.</p>
  
  <p>On certain limit I need to pause the timer which will display correct score whenever I stop the timer.</p>
  
  <p>Thank you for all of your help!!</p>