Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Javascript (http://www.velocityreviews.com/forums/f68-javascript.html)
-   -   setTimeout takes lots of processor time? (http://www.velocityreviews.com/forums/t880737-settimeout-takes-lots-of-processor-time.html)

Jorch 11-23-2004 11:41 AM

setTimeout takes lots of processor time?
 
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);
}

rf 11-23-2004 12:20 PM

Re: setTimeout takes lots of processor time?
 
Jorch wrote:
> 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.

--
Cheers
Richard.



lawrence 11-23-2004 05:54 PM

Re: setTimeout takes lots of processor time?
 
Jorch <jorchrock_1@hotmail.com> wrote in message news:<cnv7kp$hg0$1@nyytiset.pp.htv.fi>...
> 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>

Jorch 11-23-2004 08:09 PM

Re: setTimeout takes lots of processor time?
 
rf wrote:

> Jorch wrote:
>
>>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

Michael Winter 11-23-2004 10:00 PM

Re: setTimeout takes lots of processor time?
 
On 23 Nov 2004 09:54:34 -0800, lawrence <lkrubner@geocities.com> wrote:

[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

--
Michael Winter
Replace ".invalid" with ".uk" to reply by e-mail.

RobG 11-23-2004 11:47 PM

Re: setTimeout takes lots of processor time?
 
Michael Winter wrote:
> [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
:-)

--
Rob


All times are GMT. The time now is 09:03 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.