setTimeout takes lots of processor time?

J

Jorch

Hi,

Can you help me?? Why code 1 eats up 100% of processor time when browser
window is active and code 2 doesn't????

Do I use setTimeout wrong somehow???

Code 1 changes visibility of div layer and code 2 changes pictures.
Does that make difference??

Regs.
-jori luoto

<!-- THIS IS CODE 1 -->

var clc=1;
var cl=null;
var timeo=null;
var lt=new Array(3);

function LayTime(){
if(lt.length==0 && clc==0) return true;
var nl=null;
if(cl==null) nl=0;
else if(cl<clc-1) nl=cl+1;
else if(cl==clc-1) nl=0;
var d=null,nd=null,scl="d"+cl+"t",ncl="d"+nl+"t";
if(cl!=null) d=document.getElementById(scl);
nd=document.getElementById(ncl);
if(d!=null) d.style.visibility='hidden';
nd.style.visibility='visible';
cl = nl;
timeo = setTimeout("LayTime()",lt[cl]*1000);
}

function ExitPage(){
if(timeo!=null) clearTimeout(timeo);
timeo = null;
}
</script>
<link href="../sys/info.css" rel="stylesheet" type="text/css">
</head>
<body bgcolor="#000000" leftmargin="0" topmargin="0" marginwidth="0"
marginheight="0" onLoad="LayTime()" onUnLoad="ExitPage()">
<div id="c0l">
....
</div></body></html>

<!-- THIS IS CODE 2 -->

function cycleAds() {
if (document.images) {
if (document.adBanner.complete) {
if (++thisAd == adImages.length) {thisAd = 0;}
document.adBanner.src = adImages[thisAd];
}
}
// change to next sponsor every 3 seconds
setTimeout("cycleAds()", 3000);
}
 
R

rf

Jorch said:
Hi,

Can you help me?? Why code 1 eats up 100% of processor time when browser
window is active and code 2 doesn't????
timeo = setTimeout("LayTime()",lt[cl]*1000);

alert the value of lt[cl] here. I'll bet it is 0 or null.

The preceeding code is too convoluted for me to figure out.
 
L

lawrence

Jorch said:
Hi,

Can you help me?? Why code 1 eats up 100% of processor time when browser
window is active and code 2 doesn't????

Do I use setTimeout wrong somehow???

Code 1 changes visibility of div layer and code 2 changes pictures.
Does that make difference??

I'm having a similar problem. If I use document.write() in a loop,
then the browsers have no trouble. But if I use setTimeout(), the both
FireFox and IE freeze up. Then they say a script is causing them to
run slowly and they offer me the option to quit. Nothing ever gets
printed to the screen. I'm not sure why this is happening. So then I
tried using pausecomp(), which I found on the web. Just like
setTimeout, when I use it everything comes to a halt.









<html>
<head>
</head>
<body>

<script type="text/javascript">

var r = 0;
var i = 0;



for (var t=0; t < 100; t++) {
var left = Math.round(800 * Math.random());
var top = Math.round(100 * Math.random());
top = top + i;
i = i + 50;

var color1 = 1 + Math.round(8 * Math.random());
var color2 = 1 + Math.round(8 * Math.random());
var color3 = 'f'

var color = '';
color += color1;
color += color2;
color += color3;

document.write("<div id='ID" + i + "' style='position:absolute; top:"
+ top + "; left:" + left + "; padding:10px; color:#ffa;
background-color:#" + color + ";'> <h2>God loves you</h2> </div>");



if (i > 700) {
i = -100;
}
}




function pausecomp(amount) {
d = new Date();
while (1) {
mill = new Date();
diff = mill-d;
if (diff > amount) {
break;
}
}
}


for (var y=0; y < 10; y++) {

pausecomp(5000);
//document.location="about:blank";
}

</script>

</body>
</html>
 
J

Jorch

rf said:
Jorch said:
Hi,

Can you help me?? Why code 1 eats up 100% of processor time when browser
window is active and code 2 doesn't????

timeo = setTimeout("LayTime()",lt[cl]*1000);


alert the value of lt[cl] here. I'll bet it is 0 or null.

The preceeding code is too convoluted for me to figure out.
Thanx,

I'm having a project with mixed environment (javascript/java/php) sooo..

I did mixed up the constructor system of the Array var between php &
javascript, that's because i'm having an array.length==3 filled w/
zeroes while it shoud be array.length==1, array[0]==3

Thanx, I owe you a beer (in Helsinki/Finland =)

-jori
 
M

Michael Winter

[snip]
Just like setTimeout, when I use [pausecomp()] everything comes to a
halt.
[snip]

function pausecomp(amount) {
d = new Date();
while (1) {
mill = new Date();
diff = mill-d;
if (diff > amount) {
break;
}
}
}

for (var y=0; y < 10; y++) {

pausecomp(5000);
//document.location="about:blank";
}

Well, it will. Whilst the condition hasn't been met, the user agent will
be constantly executing the content of the loop, taking as much processor
time as it can.

Mike
 
R

RobG

Michael said:
[snip]
function pausecomp(amount) {
d = new Date();
while (1) {
mill = new Date();
diff = mill-d;
if (diff > amount) {
break;
}
}
}

for (var y=0; y < 10; y++) {

pausecomp(5000);
//document.location="about:blank";
}


Well, it will. Whilst the condition hasn't been met, the user agent
will be constantly executing the content of the loop, taking as much
processor time as it can.

Mike

Hey Lawrence, pausecomp() is effectively asking your processor to run
its guts out creating as many date objects as it possibly can until
enough time has elapsed so it can stop. It's guaranteed to slow any
system: the more powerful the processor, the more date objects that
will be created (and destroyed...). Most will make several with
exactly the same time.

For fun, I put a counter on the while loop to discover how many dates
are created in 1 second using pausecomp()... *69,633*!! :-o

Holly smokin' processors Batman!

Maybe it should be called "pauseUI" 'cos the computer ain't pausing,
it's going flat out!

Did you get this from a performance test harness or something? 'Cos
that's about all it's good for. But thanks for posting it, I had fun
:)
 

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,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top