Register

If this is your first visit, please click the Sign Up now button to begin the process of creating your account so you can begin posting on our forums! The Sign Up process will only take up about a minute of two of your time.

Results 1 to 7 of 7
  1. #1
    Junior Member
    Join Date
    May 2003
    Posts
    18
    Member #
    1365
    Hi all,

    Here's my problem - hope you can help:

    I have a page divided into several frames.

    At the top most frame there's a function that document.writes a few lines of text to the lower frame every 500 milisecs (using a setTimeout command that calls itself).
    This functions runs several times depending on other parameters. It might run between once to several thousands of times.

    When it gets called only a reasonable amount of times (between one to a few hundred) everything works ok. BUT when the document.writes start to mass-up (around 500 lines) the IE begins to act-up. If you just look at it, everything works fine and the lines are written to the bottom pane (cpu usage is low). But if you move the mouse around the page, scroll down, select text, wham! the cpu goes to 100% and the IE hardly responds.

    Wierd thing is, that when the function stops and no more document.write are being called, IE still continues to act up like that. Only a refresh to the page stops it.

    Any ideas?
    Thanks,
    Roy.

  2.  

  3. #2
    Senior Member splufdaddy's Avatar
    Join Date
    Feb 2003
    Location
    Boston, MA
    Posts
    4,488
    Member #
    735
    Could you post a link so we can check it out?

  4. #3
    Junior Member
    Join Date
    May 2003
    Posts
    18
    Member #
    1365
    Thanks for the reply.

    And no. Sorry.

    It is an internal mechanism (and it is still in development - I'm not the only one working on this. I just write the HTML/Javascript -- another guy writes the JAVA bit).

  5. #4
    WDF Staff smoseley's Avatar
    Join Date
    Mar 2003
    Location
    Boston, MA
    Posts
    9,729
    Member #
    819
    Liked
    205 times
    Ok, I know your problem (I think... I can't see the code, so I'm not sure).

    It's the way you're using setTimeout. You can't stack up a bunch of setTimeout functions on eachother, because they will all execute, setting the Timeout for the same instant, and will max out your cpu.

    If you want to create a 1/2 second interval, you have two options:

    Option 1: use a recursive timeout function, like so:
    Code:
    var myArray = ['string number 1', 'string number 2'];
    function timeoutLoop(i) {
        document.writeln(myArray[i] + '<br>');
        i = (i >= myArray.length - 1) ? 0 : i + 1;
        window.setTimeout('timeoutLoop(' + i + ');', 500);
    }
    timeoutLoop(0);
    The above recursive function, once initiated, will call itself, looping through the strings set in the array.

    Option 2: setInterval()
    Code:
    var i;
    var myArray = ['string number 1', 'string number 2'];
    function timeoutLoop() {
        document.writeln(myArray[i] + '<br>');
        i = (i >= myArray.length - 1) ? 0 : i + 1;
    }
    window.setInterval('timeoutLoop();', 500);
    setInterval works a little differently. It tells your browser to execute the command specified every specified period of time. Since the parameter passed would be the same for each interval, we switch the index to a global variable and persist its value rather than passing it as a param.

    EDIT: fixed a bug I noticed :-P

  6. #5
    WDF Staff smoseley's Avatar
    Join Date
    Mar 2003
    Location
    Boston, MA
    Posts
    9,729
    Member #
    819
    Liked
    205 times
    I think you were probably calling your setTimeout in a loop, like so:
    Code:
    for (i=1; i<1000000; i++) {
        window.setTimeout('myFunction();', 500);
    }
    Now, consider the output of that code. You're in one instant telling your processor to loop through and process all of the timeOuts. Remember, a timeout doesn't delay your machine, but sets the function to execute in the future... all other processing continues (this is called multi-threading). So, what you're doing is opening a million threads to execute a half second from the present. Your processor then overloads and dies on you.

    In the first example I showed you, we get around this by initiating the next thread in the execution of the delayed thread itself. This means that the second thread won't be set until the first one executes, effectively setting an interval.

    The second example I showed you uses a method MADE for setting intervals. Many people still use setTimeout to create intervals for various reasons, though.

    Hope all this explains it a little

  7. #6
    Junior Member
    Join Date
    May 2003
    Posts
    18
    Member #
    1365
    Thanks a lot for the replies. I actually use method number 2 from your examples.

    The function calls itself every 500 milisecs.. There's an IF at the beginning that breaks the loop if some conditions are met.

    Here's a snip from the function (since it is quite large).

    function checkStatus() {
    var progress;
    if (!test.isTransferInProgress()){
    progress = getProgressArgs(test.getTransferStatus());
    .
    .
    .
    .
    .
    .
    else {
    progress = getProgressArgs(test.getTransferStatus());
    .
    .
    .
    .
    .
    .
    setTimeout("checkStatus()", 500);
    }
    }


    Note that only the 'else' calls the function again.

    The function is quite large - 143 lines of code in total, that calls other functions, one of which writes to the bottom frame using document.write. Here it is:
    function fileLog(files_)
    {
    if (files_ != "") {
    if (first_line) {
    top.filelist.document.write('<font size=2 face="Tahoma"><b>Transfer Report:</b><br>');
    top.filelist.document.write('<span style="width: 220px"><font face="Tahoma" size=2><b>File name</b></span>');
    top.filelist.document.write('<span style="width: 95px; text-align: right;"><font face="Tahoma" size=2><b>Size&nbsp;(KB)&nbsp;&nbsp;&nbsp;&nbsp;&n bsp;&nbsp;</b></span>');
    top.filelist.document.write('<span style="width: 185px"><font face="Tahoma" size=2><b>Status</b></span><br>');
    // first_line = false;
    }

    var triplets = files_.split("^");
    var final_message = false;

    for(var i = 0; i < triplets.length/3; i++)
    {
    if(triplets[i*3].indexOf("-------") != "-1") final_message = true;
    var file_size;
    if(!final_message) {
    if (global_i%2 == 1) {
    top.filelist.document.write('<span style="width: 220px; background-color: #FFF7F7;" align="left"><font face="Tahoma" size=2>' + triplets[i*3] +'</span>');
    file_size = triplets[i*3+1];
    // if(file_size = 0)
    // top.filelist.document.write('<span align="right" style="width: 95px; background-color: #FFF7F7; text-align: right;"><font size=2>' + 0 + "&nbsp;&nbsp;KB&nbsp;&nbsp;&nbsp;&nbsp;</span>");
    // else {
    if(file_size > 999) {
    file_size = parseInt(file_size/1000);
    top.filelist.document.write('<span align="right" style="width: 95px; background-color: #FFF7F7; text-align: right;"><font size=2>' + file_size + "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nb sp;</span>");
    }
    else {
    if(file_size != 0)
    top.filelist.document.write('<span align="right" style="width: 95px; background-color: #FFF7F7; text-align: right;"><font size=2>1&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;</span>');
    else
    top.filelist.document.write('<span align="right" style="width: 95px; background-color: #FFF7F7; text-align: right;"><font size=2>' + file_size + "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nb sp;</span>");
    }
    top.filelist.document.write('<span style="width: 185px; background-color: #FFF7F7;" align="left"><font size=2>' + triplets[i*3+2] + "</span></tr><br>");
    global_i++;
    }
    else {
    top.filelist.document.write('<span style="width: 220px; background-color: white;" align="left"><font face="Tahoma" size=2>' + triplets[i*3] +'</span>');
    file_size = triplets[i*3+1];
    if(file_size > 999) {
    file_size = parseInt(file_size/1000);
    top.filelist.document.write('<span align="right" style="width: 95px; background-color: white; text-align: right;"><font size=2>' + file_size + "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nb sp;</span>");
    }
    else {
    if(file_size != 0)
    top.filelist.document.write('<span align="right" style="width: 95px; background-color: white; text-align: right;"><font size=2>1&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;</span>');
    else
    top.filelist.document.write('<span align="right" style="width: 95px; background-color: white; text-align: right;"><font size=2>' + file_size + "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nb sp;</span>");
    }

    top.filelist.document.write('<span style="width: 185px; background-color: white;" align="left"><font size=2>' + triplets[i*3+2] + "</span></tr><br>");
    global_i++;
    }
    }
    else {
    if(triplets[i*3].indexOf("-------") != "-1")
    top.filelist.document.write('<hr align=left style="width: 500px;">');
    else
    top.filelist.document.write('<span style="background-color= #6699FF;width: 500px"><font face="Tahoma" size=2>' + triplets[i*3] + "<br>");
    global_i++;
    }
    first_line = false;
    }
    }

    final_message = false;
    }

    Sorry if I overdeed my examples.
    Maybe it's the length of the functions that hogs the CPU? Why doesn't it happen when the main function only gets called for ~100 times then?

    Thanks a lot for your answers and sorry for the long post.
    Roy.

  8. #7
    Junior Member
    Join Date
    May 2003
    Posts
    18
    Member #
    1365
    Just wanted to let you know I solved the problem (I think).

    It wasn't the document.write after all - it was the <span> tags used to format the text on the bottom frame (inside the document.write)...
    I replaced them all with table tags and now the CPU is low and everything is moving along in an acceptable way.

    Thanks a lot everyone for the hard efforts.
    Roy.


Remove Ads

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
All times are GMT -6. The time now is 09:53 PM.
Powered by vBulletin® Version 4.2.3
Copyright © 2020 vBulletin Solutions, Inc. All rights reserved.
vBulletin Skin By: PurevB.com