javascript,tutorials

Simple Timer in javascript

28 Dec , 2008  

This tutorial shows you a simple count down timer in javascript, which you can modify according to your need.. This is in javascript, hence it is not as powerful as it should be, like if a user refreshes its browser, the value is not stored anywhere, and the countdown timer start again.

But for the added security you can store the time span in a cookie or a session variable using PHP or anyother web scripting language.

For now the javascript code is like this.

<script language='javascript'>
 var milisec=0
 var seconds=30
 var minutes=1
 //document.d2.value='20'
 
function display(){
 
 if (milisec<=0){
    milisec=9
    seconds-=1
 }
 if (seconds<=-1){
    milisec=0
	minutes-=1
    seconds=59
 
 }
 if(minutes<=0){
 document.getElementById('t1').innerHTML="Time Over";
 
 
 }
 else
    milisec-=1
    document.getElementById('t1').innerHTML=minutes+"."+seconds+"."+milisec
    setTimeout("display()",100)
}
 
</script>

this is the function display you can paste the above piece of code in the head section of your HTML page. The HTML page may contain the following lines of code.

<body onLoad="display()">
<div id='t1'>
 
</div>
</body>

This is a simple, code and you can change it to make it more powerful and attractive,
Note: This is an old post from my another blog, if you face any problem running the code, please report !!
Cheers !!
Realin !

,


One Response to “Simple Timer in javascript”

  1. Cristophe says:

    Freezes at 0.58.9 ! O.o’

More in javascript, tutorials (100 of 114 articles)


In this tutorial i am going to explain how can we hide or unhide elements on a webpage using simple ...