The pause that doesn't

A

arajunk

I was looking around for some code for a special purpose and found
this. Seems it should print "here we go" , pause 5 seconds, print "now
we are back". Instead, it pauses 5 seconds before printing both lines
with no pause between. Can someone please explain why it behaves that
way. I know in the case of "setTimeout" the only thing affected is the
function, etc. appearing as "setTimeout" arguments. But, this is just
straight code.

<body>
<script language='javascript'>
document.write('here we go<br>');
pausecomp(5000);
document.write('now we are back');

function pausecomp(Amount)
{
d = new Date() //today's date
while (1<2)
{
mill=new Date() // Date Now
diff = mill-d //difference in milliseconds
if( diff > Amount ) {break;}
}
}
</script>

</body>
 
L

Lee

(e-mail address removed) said:
I was looking around for some code for a special purpose and found
this. Seems it should print "here we go" , pause 5 seconds, print "now
we are back". Instead, it pauses 5 seconds before printing both lines
with no pause between. Can someone please explain why it behaves that
way. I know in the case of "setTimeout" the only thing affected is the
function, etc. appearing as "setTimeout" arguments. But, this is just
straight code.

<body>
<script language='javascript'>
document.write('here we go<br>');
pausecomp(5000);
document.write('now we are back');

function pausecomp(Amount)
{
d = new Date() //today's date
while (1<2)
{
mill=new Date() // Date Now
diff = mill-d //difference in milliseconds
if( diff > Amount ) {break;}
}
}
</script>

</body>


You can't expect the display to be updated by the time the document.write() call
returns. That would cause trouble if you wanted to write part of a block in one
call and the rest in a subsequent call, as is not uncommon.

You can't be sure that the document will updated until you invoke
document.close(), although some browsers will make assumptions about when you're
done.

And you certainly can't expect the browser to update the page when your machine
is wasting 100% of its CPU capacity spinning its wheels waiting for 5 seconds to
pass.

If you want a delay, use setTimeout()
 
F

Fred Oz

I was looking around for some code for a special purpose and found
this. Seems it should print "here we go" , pause 5 seconds, print "now
we are back". Instead, it pauses 5 seconds before printing both lines
with no pause between. Can someone please explain why it behaves that
way. I know in the case of "setTimeout" the only thing affected is the
function, etc. appearing as "setTimeout" arguments. But, this is just
straight code.

<body>
<script language='javascript'>
document.write('here we go<br>');
pausecomp(5000);
document.write('now we are back');

function pausecomp(Amount)
{
d = new Date() //today's date
while (1<2)
{
mill=new Date() // Date Now
diff = mill-d //difference in milliseconds
if( diff > Amount ) {break;}
}
}
</script>

</body>

This is effectively a denial of service attack. It will hog the
CPU by getting it to generate 2 or 3 dates per millisecond (or
maybe 10 or 15 on faster machines than my slow old laptop)
waiting for your script to timeout.

I'm not sure that is what Javascript is for.
 
@

@(none)

Fred said:
This is effectively a denial of service attack. It will hog the
CPU by getting it to generate 2 or 3 dates per millisecond (or
maybe 10 or 15 on faster machines than my slow old laptop)
waiting for your script to timeout.

I'm not sure that is what Javascript is for.
Have a look at "http://www.synchro.net/docs/jsobjs.html" and search for
mswait.

I don't know that there is an inbuilt sleep or wait function in
javascript ( wait for flames !! ) - the secret is to let the CPU do
other things during the sleep period - this may work.
 
M

Michael Winter

[snip]
Have a look at "http://www.synchro.net/docs/jsobjs.html" and search for
mswait.

It's a fairly safe bet that the reference there is specifically for the
Synchronet BBS system. There certainly isn't a mswait function in any
browser object model that I'm aware of.
I don't know that there is an inbuilt sleep or wait function in
javascript

No, there isn't. As both Lee and Fred said, a feature such as that would
cause a browser to hang in most cases as script execution is synchronous.
the secret is to let the CPU do other things during the sleep period -
this may work.

Precisely. Place the to-be-delayed code in a function and use setTimeout
to call it after a certain period of time. However, this couldn't be done
with document.write as the document stream would have closed resulting in
replacement of, rather than addition to, the page.

Mike
 
A

arajunk

From what I read here I can't do what I want with JS. Using a form
textarea or the status line I can do it.... but, I don't care for
either. I do have a java applet that will work. The application is
very simple. Display a small bit of information followed by a pause
and then display anothr bit of information, pause..... etc. Looping
through an array . Anybody know how to do it?
 
L

Lee

(e-mail address removed) said:
textarea or the status line I can do it.... but, I don't care for
either. I do have a java applet that will work. The application is
very simple. Display a small bit of information followed by a pause
and then display anothr bit of information, pause..... etc. Looping
through an array . Anybody know how to do it?

<html>
<head>
<title>demo</title>
<script type="text/javascript">

var info = [
"From what I read here I can't do what I want with JS. Using a form",
"textarea or the status line I can do it.... but, I don't care for",
"either. I do have a java applet that will work. The application is",
"very simple. Display a small bit of information followed by a pause",
"and then display anothr bit of information, pause..... etc. Looping",
"through an array . Anybody know how to do it?"
];

function showInfo() {
info.pos|=0;
document.getElementById("canvas").innerHTML=info[info.pos];
info.pos=(info.pos+1)%info.length;
info.timer=setTimeout("showInfo()",2000);
}

</script>
</head>
<body onload="showInfo()">
<div id="canvas" style="padding:1em;background-color:yellow"></div>
</body>
</html>
 

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,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top