Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > setTimeout takes lots of processor time?

Reply
Thread Tools

setTimeout takes lots of processor time?

 
 
Jorch
Guest
Posts: n/a
 
      11-23-2004
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);
}
 
Reply With Quote
 
 
 
 
rf
Guest
Posts: n/a
 
      11-23-2004
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.


 
Reply With Quote
 
 
 
 
lawrence
Guest
Posts: n/a
 
      11-23-2004
Jorch <(E-Mail Removed)> wrote in message news:<cnv7kp$hg0$(E-Mail Removed)>...
> 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>
 
Reply With Quote
 
Jorch
Guest
Posts: n/a
 
      11-23-2004
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
 
Reply With Quote
 
Michael Winter
Guest
Posts: n/a
 
      11-23-2004
On 23 Nov 2004 09:54:34 -0800, lawrence <(E-Mail Removed)> 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.
 
Reply With Quote
 
RobG
Guest
Posts: n/a
 
      11-23-2004
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*!!

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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Looking for lots of words in lots of files brad Python 9 06-19-2008 07:59 AM
Downloading lots and lots and lots of files coolneo Perl Misc 9 01-30-2007 02:34 PM
AMD 64 X2 Processor: Any what to tell what program/process is assigned to processor? The Frozen Canuck Windows 64bit 1 01-16-2006 07:45 PM
Processor fried, should I upgrade or just buy a processor? Dim Computer Support 6 06-21-2004 08:11 PM
setExpression takes client processor to 100% Jack Wright Javascript 1 02-12-2004 03:13 PM



Advertisments