/    Sign up×
Community /Pin to ProfileBookmark

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, [I]then[/I] 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?

to post a comment
JavaScript

5 Comments(s)

Copy linkTweet thisAlerts:
@ibidris2003Aug 30.2003 — 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/
Copy linkTweet thisAlerts:
@FangAug 30.2003 — This runs DoSomething ONCE after 5 seconds:
<i>
</i>var timerID=setTimeout("DoSomething()",5000);
// clear up afterwards
clearTimeout(timerID);

This runs DoSomething EVERY 5 seconds untill clearInterval is called:
<i>
</i>var timerID = setInterval("DoSomething()",5000)
clearInterval(timerID);
Copy linkTweet thisAlerts:
@BritMissauthorAug 30.2003 — Now I'm even more mystified! I tried the code supplied by Fang in the following script

[CODE]<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>[/CODE]


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

Is this something peculiar to IE6? can anybody suggest a reason why this is happening?
Copy linkTweet thisAlerts:
@CharlesAug 30.2003 — [font=georgia]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.[/font]
Copy linkTweet thisAlerts:
@Khalid_AliAug 30.2003 — 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>
×

Success!

Help @BritMiss spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.16,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...