Stop countdown timer when it reaches to "0" - Javascript Browser Object Model

Javascript examples for Browser Object Model:Window setInterval

Description

Stop countdown timer when it reaches to "0"

Demo Code

ResultView the demo in separate window

<html>
   <head> 
      <meta name="viewport" content="width=device-width, initial-scale=1"> 
      <script type="text/javascript">
    window.onload=function(){//from w  w  w  . j a v  a2  s. co m
var i = document.getElementById('counter'),
    sId;

function countdown() {
    var count = parseInt(i.textContent, 10);
    if (count < 1) {
        clearInterval(id);
    }
    i.textContent = count - 1;
}
sId = setInterval(countdown, 1000);

    }

      </script> 
   </head> 
   <body> 
      <div id="counter">
         10
      </div>  
   </body>
</html>

Related Tutorials