©ilj@flowsim.se
JavaScript code samples 20
JavaScript timer - setTimeout, clearTimeout
 - timer in text box or in the status bar
>>> this example in Swedish



 
stop!
restart!


code and explanations further down




 
This is what the code looks like:
 

<SCRIPT LANGUAGE="
JavaScript"
TYPE="
text/javascript">
<!--

startad=0
iljdone=0
function checkstart()
{
if(startad==0)
{
startad=1
j=0
i=0
iljtimer4()
}
}
i=0
j = 0
function iljtimer4()
{
if(iljdone<600)
{
iljtimer = setTimeout("iljtimer4()",1000)
if(i<10)i="0"+i
window.status =" text: " + j + ":" + i
//1
document.iljtimerform
2.iljtimerruta3.value =j+ ":"+ i //4
i++
if(i==60)
{
i = 0
j ++
}
iljdone++
}
}
function stoppit()
{
clearTimeout(iljtimer)
startad=0
iljdone=0
}
window.onerror = null;
//
-->
</SCRIPT>

 

<FORM NAME = "
iljtimerform2">
<INPUT TYPE = "
text"
NAME = "
iljtimerruta3"
SIZE = "
10">
</FORM>
<A HREF = "
#"
ONCLICK="
stoppit();return false">STOP</A>

<A HREF = "
#"
ONCLICK="
checkstart();return false">RESTART</A>

 

 
If you copy the code - donīt forget to remove the footnotes!

 

 
1 this line puts a timer in the status bar (window.status)
2 the name of the
FORM
3 the name of the text box
4 this line puts the timer in a text box named
iljtimerruta on a FORM named iljtimerform

 

 
Put the script within SCRIPT tags in the HEAD part of the document (i.e. somewhere between the HEAD tags).
If you want the timer to start as soon as the page is loaded you can put the call
checkstart() after BODY ONLOAD.
This timer stops automatically after 10 minutes; change the number after
iljdone , (600 in this example) to have it function for shorter or longer period.
 
The following code puts the timer on a div named "iljs" (<div id='iljs'> </div>)
 
if(document.getElementById)i
{document.getElementById('iljs').innerHTML=j + ":"+ i}

 

 


OTHER JAVASCRiPT EXAMPLES
 
How to use javascript to choose what image to show by selecting it in an option list
How to use javascript to switch images
How to use javascript to switch images onmouseover
JavaScript getDay() and document.write
JavaScript date
JavaScript timer
How to use javascript to switch images onmouseover
JavaScript timer
JavaScript onmouseover
JavaScript lastModified
JavaScript/ text in text area
JavaScript/ text in text area
how to open new windows in JavaScript
JavaScript clock
JavaScript confirm
JavaScript alert
JavaScript prompt
JavaScript onmouseover
JavaScript onmouseover2
JavaScript animation
JavaScript animation 2
 

Page by
 
ilj
 
ilj@flowsim.se
 

 
Last modified: August 17, 1998
 
This page should be part of a frames system
at:
http://www.flowsim.se
JAVASCRiPT