JavaScript Tutorial JavaScript Examples CSS Tutorial HTML Tutorial About Us

Javascript Tutorial


We create a countdown timer with the following JavaScript

<script>
function countdown(){

var counter = 10;
var newYearCountdown = setInterval(function(){
document.getElementById("demo").innerHTML = counter;
counter--
if (counter ==-1) {
document.getElementById("demo").innerHTML = "HAPPY NEW YEAR!!";
clearInterval(newYearCountdown);
}
}, 1000);

}
</script>

The setInterval method is to set the interval of the timer, 1000 means 1 second. Besides that, we use the if statement to decrease the counter by 1 after each cycle.

10



Copyright©2008 Dr.Liew Voon Kiong. All rights reserved |Contact|Privacy Policy