473,386 Members | 1,810 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

The pause that doesn't

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
7 4522
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
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
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
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
>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
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
Thanks Lee !! Nice job.

Jul 23 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Andrew | last post by:
When will .NET have a low-pause-time garbage collector A low-pause-time garbage collector would greatly improve .NET's ability to serve as a platform for soft real-time systems. It doesn't have...
11
by: Paminu | last post by:
Is there something like system("PAUSE") for linux?
8
by: Wim | last post by:
My GUI application starts a process (a console program) when the user hits Play. I would like to add an option to pause that process. The code I've added to detect if the user hit pause/unpause...
11
by: Marek | last post by:
What can I do in order to avoid first exception pause? I am sure everyone already experienced this behavior and there must be a solution. regards
2
by: allen | last post by:
I'm having a problem in that the first time an exception is thrown in my app, there is about a 2 second pause. After that first time, subsequent exceptions go very quickly. This pause is really...
38
by: Jackie | last post by:
I just want the programme to stop for a while. Thanks.
1
by: OBINT | last post by:
Hi I am using one API to do some process for me, but unfortunately API dont have a option of pause the process and resume it back. How can i do this using C# API take more then 2 hours in...
0
by: Arik | last post by:
Hi, I would like to disable the 'Pause/Break' key in a console application. Normally, pressing the 'Pause' key halts the application's execution and may lead to undesirable processing gaps in my...
2
by: James | last post by:
anyone know why the pause button doesn't work in vb.net 2003? it works on some small applications but not this large one im currently working on. i will appreciate any ideas!
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.