setTimeout takes lots of processor time?

Discussion in 'Javascript' started by Jorch, Nov 23, 2004.

  1. Jorch

    Jorch Guest

    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);
    }
    Jorch, Nov 23, 2004
    #1
    1. Advertising

  2. Jorch

    rf Guest

    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.
    rf, Nov 23, 2004
    #2
    1. Advertising

  3. Jorch

    lawrence Guest

    Jorch <> wrote in message news:<cnv7kp$hg0$>...
    > 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>
    lawrence, Nov 23, 2004
    #3
  4. Jorch

    Jorch Guest

    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
    Jorch, Nov 23, 2004
    #4
  5. On 23 Nov 2004 09:54:34 -0800, lawrence <> 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.
    Michael Winter, Nov 23, 2004
    #5
  6. Jorch

    RobG Guest

    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
    RobG, Nov 23, 2004
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. flamesrock
    Replies:
    8
    Views:
    434
    Hendrik van Rooyen
    Nov 24, 2006
  2. brahatha
    Replies:
    1
    Views:
    645
  3. brad
    Replies:
    9
    Views:
    353
    Bruno Desthuilliers
    Jun 19, 2008
  4. Jack Wright

    setExpression takes client processor to 100%

    Jack Wright, Feb 12, 2004, in forum: Javascript
    Replies:
    1
    Views:
    89
    Richard Cornford
    Feb 12, 2004
  5. coolneo
    Replies:
    9
    Views:
    177
    coolneo
    Jan 30, 2007
Loading...

Share This Page