Javascript Countdown Timer Code - Javascript
How to create simple countdown timer using javascript code?
Snippet Code
The following javascript code can be used to create countdown timer using countdown() function.
<div id='time'></div>
<script type='text/javascript'>
var hour = '01';
var min = '05';
var sec = '10';
function countdown() {
if(sec <= 0 amp&min > 0) {
sec = 59;
min -= 1;
}
else if(min <= 0 amp&sec <= 0) {
min = 0;
sec = 0;
}
else {
sec -= 1;
}
if(min <= 0 amp&hour > 0) {
min = 59;
hour -= 1;
}
var pattern = /^[0-9]{1}$/;
sec = (pattern.test(sec) == true) ? '0'+sec : sec;
min = (pattern.test(min) == true) ? '0'+min : min;
hour = (pattern.test(hour) == true) ? '0'+hour : hour;
document.getElementById('time').innerHTML = hour+" hrs: "+min+" mins: "+sec+" secs ";
if (hour=='00' amp&min=='00' amp&sec=='00') {
alert("Times Out!");
}
else{
SD=window.setTimeout("countdown()",1000);
}
}
countdown();
</script>
Tags