Problem getting a timed delay

W

whitesmith

I've read the FAQ about means of implementing a delay, but nothing
seems to work for me. My HTML page looks like this:

<html>
<head>
.... usual html stuff and then:
<script type="text/javascript">
function pause( milliseconds )
{
var date = new Date();
var curDate = null;
do
{ curDate = new Date(); }
while( curDate - date < milliseconds );
}
</script>
</head>

And then I do something like:
<body>
<p>
Now is the time
<script type="text/javascript">
pause( 5000 );
</script>
for all good men ...
</p>
</body>
</html>

My intention is to have "Now is the time" print followed by a 5-second
delay followed by "for all good men ..."

Sadly what actually happens is that the entire webpage delays for 5
seconds before loading and then "Now is the time for all good men ..."
prints as a unit. Can anyone tell me what I'm doing wrong before I go
insane? Many thanks for any and all help.
 
R

Randy Webb

whitesmith said the following on 2/27/2007 2:56 PM:
I've read the FAQ about means of implementing a delay, but nothing
seems to work for me.

If you read the FAQ about it then why didn't you implement the methods
in the FAQ? And, the FAQ also says the way you tried is a bad idea.

<quote>
Achieving delays through running a loop of some sort for a pre-defined
period is a bad strategy, as that will tend to put CPU use up to 100%
and inhibit whatever was supposed to be happening during the delay.
</quote>

Which is exactly the behavior you are getting.
My intention is to have "Now is the time" print followed by a 5-second
delay followed by "for all good men ..."

function addText(){
document.getElementById('extraText').innerHTML="for all good men"
}

var timer=window.setTimeout('addText()',5000)

<div>Now is the time</div>
<div id="extraText"></div>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top