I have a javascript that displays a countdown in the browser. The problem is that if the element does not load immediately the countdown turns to negative. Here is the script:
<script>
var time = 10; //How long (in seconds) to countdown
var page = '<?php echo $page ?>'; //The destination.
function countDown(){
time --;
get("message").innerHTML = time;
if(time == 0){
window.location = page;
}
}
function get(id){
if(document.getElementById) return document.getElementById(id);
if(document.all) return document.all.id;
if(document.layers) return document.layers.id;
if(window.opera) return window.opera.id;
}
function init(){
if(get('message')){
setInterval(countDown, 1000);
get("message").innerHTML = time;
}
else{
setTimeout(init, 50);
}
}
document.onload = init();
</script>
Will someone please help? I just need a way to tell the countdown to stop at 0. Thanks.