473,396 Members | 1,815 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,396 software developers and data experts.

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);
}
Jul 23 '05 #1
5 2119
rf
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.
Jul 23 '05 #2
Jorch <jo*********@hotmail.com> wrote in message news:<cn**********@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>
Jul 23 '05 #3
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
Jul 23 '05 #4
On 23 Nov 2004 09:54:34 -0800, lawrence <lk******@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.
Jul 23 '05 #5
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
Jul 23 '05 #6

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

Similar topics

29
by: Mic | last post by:
Goal: delay execution of form submit Code (Javascript + JScript ASP): <% Response.Write("<OBJECT ID='IntraLaunch' STYLE='display : none' WIDTH=0 HEIGHT=0...
2
by: Athanasius | last post by:
Could someone shed some light as to why the following setTimeout function will not work on the Mac IE5.2? It does however work on PC(Forefox,Netscape,IE) & Mac(Safari,Firefox). Here is the script,...
8
by: VA | last post by:
Suppose I have setTimeout(foo(),n); long_running_function(); Would foo() be executed even when long_running_function() is still executing? In other words, does the setTimeout() pre-empt...
10
by: Amelyan | last post by:
It takes VS 2005 1, 2, and somtimes 3 MINUTES to compile ASP.NET 2.0 website when external DLL is modified. As a matter of fact, when I click 'Build', it looks like VS 2005 hangs for most of that...
5
by: Ivo | last post by:
Hi, somewhere in my code a timeout is set in a function: var timer = null; function foo( n ) { var delay = 500; // do stuff with n timer = window.setTimeout( 'foo(' + ( ++n ) + ');', delay );...
3
by: jshanman | last post by:
I've created a "Play" button for my timeline that uses the Google Maps API. It basicly scrolls the timeline x interval every y milliseconds. The problem is the browser responds slowly even after...
9
by: pengypenguin | last post by:
As we know, Javascript suffers an insufferable lack of a wait() or sleep() function, and so setTimeOut must step in to take up the slack. A function can use setTimeOut like sleep() by calling...
1
by: bizt | last post by:
Hi, I have a process where every so often I refresh a table's data using a setTimeout() .. and when its no longer needed, a clearTimeout(). The following is a simple example of how this is...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.