By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,263 Members | 2,640 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,263 IT Pros & Developers. It's quick & easy.

The pause that doesn't

P: n/a
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>

Jul 23 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
Lee
ar*****@hiwaay.net 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()

Jul 23 '05 #2

P: n/a
ar*****@hiwaay.net wrote:
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.

--
Fred
Jul 23 '05 #3

P: n/a
Fred Oz wrote:
ar*****@hiwaay.net wrote:
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.

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.
Jul 23 '05 #4

P: n/a
On Sun, 09 Jan 2005 13:30:17 +1100, @(none) <<""mas"@(none)">> wrote:

[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

--
Michael Winter
Replace ".invalid" with ".uk" to reply by e-mail.
Jul 23 '05 #5

P: n/a
>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?

Jul 23 '05 #6

P: n/a
Lee
ar*****@hiwaay.net said:
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?


<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>

Jul 23 '05 #7

P: n/a
Thanks Lee !! Nice job.

Jul 23 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.