title: Why clearInterval not clearing the setInterval timer
body: <p>I have creating an simple Timer service. In the timer service, I am monitoring the user for each minute.</p>
  
  <p>While page load, have set 3 minutes to get an alert to logout and before 1 minute which means 2 minutes  to get a bootstrap warning while click  the proceed button,it will clear the current intervals and again assign 3 minutes, else, get an warning alert to logout after 3 minutes.</p>
  
  <p>For the first time its working fine, while clicking the proceed button, its not reassign the value for the user. For the 3rd minute itself, its showing the logout.</p>
  
  <p>Where is wrong in the piece of code?</p>
  
  <pre><code>&lt;!DOCTYPE html&gt;
  &lt;html&gt;
  
    &lt;head&gt;
      &lt;link data-require="bootstrap-css@4.0.0-alpha.4" data-semver="4.0.0-alpha.4" rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.4/css/bootstrap.min.css" /&gt;
     &lt;script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.4/jquery.js" &gt;&lt;/script&gt;
      &lt;script data-require="bootstrap.js@3.3.6" data-semver="3.3.6" src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"&gt;&lt;/script&gt;
      &lt;link rel="stylesheet" href="style.css" /&gt;
      &lt;script src="script.js"&gt;&lt;/script&gt;
    &lt;/head&gt;
  
    &lt;body onload="TimerService()"&gt;
      &lt;p id="demo"&gt;&lt;/p&gt;
      &lt;h1 id="title"&gt;Javascript timer problem&lt;/h1&gt;
      &lt;div id="modeldialog"&gt;&lt;/div&gt;
      &lt;div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" data-backdrop="static"&gt;
        &lt;div class="modal-dialog"&gt;
          &lt;div class="modal-content"&gt;
            &lt;div class="modal-header"&gt;
              &lt;button type="button" class="close" data-dismiss="modal" aria-hidden="true"&gt;×&lt;/button&gt;
              &lt;label class="session-expiration"&gt;
                &lt;span&gt;Session Expiration&lt;/span&gt;
                &lt;img src="~/Images/Warning_Alert_Icon_26.png" /&gt;
              &lt;/label&gt;
            &lt;/div&gt;
            &lt;div class="modal-body"&gt;
              &lt;div class="session-message"&gt;
                &lt;p class="time-message"&gt;your Session will expire in                                  &lt;i id="timer" class="fa fa-clock-o"&gt;
                    &lt;span class="expire"&gt;Minutes &lt;/span&gt;
                  &lt;/i&gt;
  
  .click proceed to stay Login.&lt;/p&gt;
                &lt;p class="time-message"&gt;
                  &lt;b&gt;Click Proceed to stay Login.&lt;/b&gt;
                &lt;/p&gt;
              &lt;/div&gt;
            &lt;/div&gt;
            &lt;div class="modal-footer"&gt;
              &lt;button type="button" class="btn btn-default" onclick="LogOut()"&gt;Logout&lt;/button&gt;
              &lt;button type="button" class="btn btn-primary" onclick="proceed()"&gt;Proceed&lt;/button&gt;
            &lt;/div&gt;
          &lt;/div&gt;
        &lt;/div&gt;
      &lt;/div&gt;
    &lt;/body&gt;
  
  &lt;/html&gt;
  </code></pre>
  
  <p><strong>Javascript:</strong></p>
  
  <pre><code>function proceed()
   {
   $("#myModal").toggle();
   TimerService();
   }
  
  function load(){
    alert(" if click proceed , timer service extended another 3 minutes else logout after 3 ");
     $("#myModal").modal();
  
    }
  
   function TimerService()
     {
     alert("TimerService");
      var timer = false;
      clearInterval(timer); 
      if (timer == false) {
          var lastDigestRun = new Date();   
             lastDigestRun = Date.now();
              var s = lastDigestRun + 3 * 60 * 1000;
              // for reference
              document.getElementById("demo").innerHTML =  new Date(s);
              console.info("Your logout time +" + new Date(s));
  
                 timer=setInterval(function () {                   
                    var now = Date.now();
                     var displaytime = now - lastDigestRun &gt; 2 * 60 * 1000;
  
                      if (now - lastDigestRun &gt; 2 * 60 * 1000) {                      
                         load();
                         }
  
                        if (now - lastDigestRun &gt; 3 * 60 * 1000) {
                          alert("logout");
  
                          }     
                  }, 60 * 1000);
  
          }
  }
  </code></pre>
  
  <p>Plunker Demo:</p>
  
  <p><a href="https://plnkr.co/edit/qwNJF648LIvUUNT3IMkc?p=preview" rel="nofollow noreferrer">https://plnkr.co/edit/qwNJF648LIvUUNT3IMkc?p=preview</a></p>