Click to See Complete Forum and Search --> : delay timer


BritMiss
08-30-2003, 04:06 AM
Sorry, it's meeee again :(

Trying to figure out how to incorporate a delay into a loop without simply using a clunky delay-loop that ties up and eventually crashes the system.

I've tried setTimeout("function()",delay) which works OK until it's called the second time, then the HTML for the whole page is reduced to the line which changes in the "function()"

And if I try to reset the timeout using clearTimeout(delay) the function is only called once, the page status "done, but with errors on page".

Am I misunderstanding the setTimeout method?
Is there a more appropriate way to incorporate a delay?
Or am I just missing something typically obvious?

ibidris2003
08-30-2003, 06:19 AM
No, you are doing it ok. You say that it crashes the system maybe you have chosen the delay too short. If the delay is 500 ms you can keep the process going for hours with a reasonably fast computer.

http://galileo.spaceports.com/~ibidris/

Fang
08-30-2003, 06:39 AM
This runs DoSomething ONCE after 5 seconds:

var timerID=setTimeout("DoSomething()",5000);
// clear up afterwards
clearTimeout(timerID);

This runs DoSomething EVERY 5 seconds untill clearInterval is called:

var timerID = setInterval("DoSomething()",5000)
clearInterval(timerID);

BritMiss
08-30-2003, 07:18 AM
Now I'm even more mystified! I tried the code supplied by Fang in the following script

<HTML>
<HEAD>
<SCRIPT language="javascript">
function DoSomething() {
document.writeln( "Doing it!<P></P>" )
}
</SCRIPT>
</HEAD>
<BODY>
<SCRIPT language="Javascript">
var timerID = setInterval("DoSomething()",5000)
clearInterval(timerID)
</SCRIPT>
</BODY>

which printed the expected message on screen once only. When I right-clicked to check the source again, it had been reduced to...
Doing it!<P></P>

Is this something peculiar to IE6? can anybody suggest a reason why this is happening?

Charles
08-30-2003, 07:44 AM
1) That's not a valid piece of HTML.

2) Once the parsing of a document is complete the input stream is closed. Any calls to document.write() or document .writeln() will first clear the entire document.

3) If you want to do something just once then use setTimeout() instead of setInterval().

4) See http://devedge.netscape.com/library/manuals/2000/javascript/1.3/reference/window.html#1203669, http://devedge.netscape.com/library/manuals/2000/javascript/1.3/reference/window.html#1203758 and http://devedge.netscape.com/library/manuals/2000/javascript/1.3/reference/document.html#1221642.

Khalid Ali
08-30-2003, 09:00 AM
post your code to explain what is it you are trying to do.setTimeout works in most situations,

PS
document.write should not be used once the document is done loading.and if you have no idea what the above phrase means then don't use it at all,instead have a div element in the document, and replace its contents with setTimeout function over and over...

below is a complete example

<script type="text/javascript">
<!--
var secCtr;
var ctr =0;
function Start(){
document.getElementById("div_1").innerHTML = ctr++;
secCtr =setTimeout('Start()',1000); //1000 milli seconds = 1 second
}
function Stop(){
clearTimeout(secCtr);
ctr=0;
return false;

}
//-->
</script>
</head>
<body>
<div id="div_1"></div>

<form id="form1" action="" onsubmit="">
<Click below to start or stop a seconds counter.<br/>
<input type="button" value="Start Counting" name="start_control" onclick="Start()"/>&nbsp;
<input type="button" value="Stopt Counting" name="stop_control" onclick="Stop()"/>
</form>