title: Stopwatch in JavaScript
body: <p>Before marking the question duplicate, I want to tell that I have been through all the stopwatch and JavaScript searches but as I am new to the JavaScript, so I can not come to the possible solution myself and I need the help from you guys.</p>
  
  <p>What I want to achieve is to start and stop the watch with the same button. I can stop the watch but can not start again, can't figure out why.</p>
  
  <p>Have a look at the following script and correct me.</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>var startTimer = setInterval(function(){myTimer()}, 1000);
  
  function myTimer(){
    var current = new Date();
    document.getElementById("timer").innerHTML = current.toLocaleTimeString();
  }
  
  function start(st){
    // Problem is in this statement
    // How can I call the global function variable again that's startTimer
    window[st]();
    var elem = document.getElementById("myButton");
    elem.innerHTML = "Stop";
    elem.addEventListener("click", stop);
  }
  
  function stop(){
    clearInterval(startTimer);
    var elem = document.getElementById("myButton");
    elem.innerHTML = "Start";
    elem.addEventListener("click", start(startTimer));
  }</code></pre>
  <pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;p id="timer"&gt;&lt;/p&gt;
  &lt;button id="myButton" onclick="stop(startTimer)"&gt;Stop&lt;/button&gt;</code></pre>
  </div>
  </div>
  </p>