small script --> huge load --> error message

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Robi

    small script --> huge load --> error message

    Ok, I'm sure everybody who works with javascript has seen this
    or similar messages depending on their agent:

    A script on this page is causing mozilla to run slowly.
    If it continues to run, your computer may become unresponsive.
    Do you want to abort the script?
    [ OK ] [Cancel]

    besides the very confusing OK/Cancel buttons in FireFox, is there a
    way to tell a javascript to give control back to the agent interface
    for a few moments?

    I am trying to list "all" the characters in tables (from #x0000-#xffff)
    yeah, that's 65K characters...

    the function is defined in the <head> section and goes:

    <script type="text/javascript">

    function createlist()
    {
    var hex="0123456789 abcdef";
    hex=hex.split(" ");
    var fourth, third, second, first, spezial;
    var oBody=document. getElementsByTa gName("body").i tem(0);
    var oTable, oTHead, oTBody, oTFoot, oCaption;
    var oRow, oCell, oHell, oDiv;

    for (first=0;first< =15;first++)
    {
    for (second=0;secon d<=15;second++ )
    {
    oTable = document.create Element("table" );
    oTHead = document.create Element("thead" );
    oTBody = document.create Element("tbody" );
    oTFoot = document.create Element("tfoot" );
    oCaption = document.create Element("captio n");

    oTable.appendCh ild(oCaption);
    oTable.appendCh ild(oTHead);
    oTable.appendCh ild(oTBody);
    oTable.appendCh ild(oTFoot);
    oTable.border=1 ;

    oRow=document.c reateElement("t r");
    oTHead.appendCh ild(oRow);
    oCell = document.create Element("th");
    oCell.appendChi ld(document.cre ateTextNode("He x"));
    oRow.appendChil d(oCell);
    oBody.appendChi ld(oTable);
    for (oHell=0;oHell< 16;oHell++){
    oCell = document.create Element("th");
    oCell.appendChi ld(document.cre ateTextNode(hex[oHell]));
    oRow.appendChil d(oCell);
    }
    oTBody = document.create Element("tbody" );
    oTable.appendCh ild(oTBody);

    for (third=0;third< =15;third++)
    {
    oRow=document.c reateElement("t r");
    oTBody.appendCh ild(oRow);
    oCell=document. createElement(" td");
    oCell.appendChi ld(document.cre ateTextNode("&# x"+hex[first]+hex[second]+"n"+hex[third]+";"));
    oRow.appendChil d(oCell);
    for (fourth=0;fourt h<=15;fourth++ )
    {
    oCell=document. createElement(" td");
    oCell.innerHTML ="&#x"+hex[first]+hex[second]+hex[fourth]+hex[third]+";";
    oRow.appendChil d(oCell);
    }
    }
    }
    }
    }

    </script>


    and then in the body I call

    <script type="text/javascript">
    createlist();
    </script>

    plain and simple, well, except for the load it creates iterating through 16^4
    characters creating 16^2 tables and 16^4+16^2 cells and 16^3+16^2 rows
    (rough calculations off the top of my head ;-)

    Is there a way to diffuse the load and still getting all the tables created?
  • RobG

    #2
    Re: small script --&gt; huge load --&gt; error message

    Robi wrote:[color=blue]
    > Ok, I'm sure everybody who works with javascript has seen this
    > or similar messages depending on their agent:
    >
    > A script on this page is causing mozilla to run slowly.
    > If it continues to run, your computer may become unresponsive.
    > Do you want to abort the script?
    > [ OK ] [Cancel]
    >
    > besides the very confusing OK/Cancel buttons in FireFox, is there a
    > way to tell a javascript to give control back to the agent interface
    > for a few moments?[/color]

    You may find this enlightening (and sympathetic too):

    <URL:http://www.fourmilab.c h/fourmilog/archives/2005-08/000568.html>

    The short answer is:

    1. Type "about:conf ig" in the address bar

    2. Scroll down to "dom.max_script _run_time"

    3. Double-click it and set it to however many seconds you'd like Firefox
    to wait before presenting the confirm dialog.
    [color=blue]
    >
    > I am trying to list "all" the characters in tables (from #x0000-#xffff)
    > yeah, that's 65K characters...
    >
    > the function is defined in the <head> section and goes:
    >
    > <script type="text/javascript">[/color]

    Using DOM is commendable, but for this task I think you will find that
    good 'ol non-standard 'DOM 0' document.write is hugely faster.

    Excuse me for not testing it! I'll have a go and post back later...

    [...]

    --
    Rob

    Comment

    • RobG

      #3
      Re: small script --&gt; huge load --&gt; error message

      RobG wrote:[color=blue]
      > Robi wrote:
      >[color=green]
      >> Ok, I'm sure everybody who works with javascript has seen this
      >> or similar messages depending on their agent:
      >>
      >> A script on this page is causing mozilla to run slowly.
      >> If it continues to run, your computer may become unresponsive.
      >> Do you want to abort the script?
      >> [ OK ] [Cancel]
      >>
      >> besides the very confusing OK/Cancel buttons in FireFox, is there a
      >> way to tell a javascript to give control back to the agent interface
      >> for a few moments?[/color]
      >
      >
      > You may find this enlightening (and sympathetic too):
      >
      > <URL:http://www.fourmilab.c h/fourmilog/archives/2005-08/000568.html>
      >
      > The short answer is:
      >
      > 1. Type "about:conf ig" in the address bar
      >
      > 2. Scroll down to "dom.max_script _run_time"
      >
      > 3. Double-click it and set it to however many seconds you'd like Firefox
      > to wait before presenting the confirm dialog.
      >[color=green]
      >>
      >> I am trying to list "all" the characters in tables (from #x0000-#xffff)
      >> yeah, that's 65K characters...
      >>
      >> the function is defined in the <head> section and goes:
      >>
      >> <script type="text/javascript">[/color]
      >
      >
      > Using DOM is commendable, but for this task I think you will find that
      > good 'ol non-standard 'DOM 0' document.write is hugely faster.
      >
      > Excuse me for not testing it! I'll have a go and post back later...[/color]

      Try this one:
      - closing tags for tr & td omitted
      - unused head, foot and caption elements omitted
      - unneccessary (in this case) tbody omitted


      function createlist() {
      var hex="0123456789 abcdef";
      hex=hex.split(" ");
      var n = hex.length;
      var fourth, third, second, first, spezial;
      var oBody = document.body;
      var txt=[];

      for ( first=0; first<n; first++ ) {
      for (second=0;secon d<n;second++) {
      txt.push('<tabl e border="1"><tr> <th>Hex<');

      for ( oHell=0; oHell<n; oHell++){
      txt.push('<th>' + hex[oHell]);
      }

      for ( third=0; third<n; third++) {
      txt.push('<tr>< td>' + '&#x' + hex[first]
      + hex[second] + 'n' + hex[third] + ';');

      for ( fourth=0; fourth<n; fourth++) {
      txt.push('<td>' + '&#x' + hex[first]
      + hex[second] + hex[fourth] + hex[third] + ';');
      }
      }
      txt.push('</table>');
      }
      }
      document.write( txt.join(''));
      }


      --
      Rob

      Comment

      • Robi

        #4
        Re: small script --&gt; huge load --&gt; error message

        RobG wrote:[color=blue]
        > RobG wrote:[color=green]
        >> Robi wrote:
        >>[color=darkred]
        >>> Ok, I'm sure everybody who works with javascript has seen this
        >>> or similar messages depending on their agent:
        >>>
        >>> A script on this page is causing mozilla to run slowly.
        >>> If it continues to run, your computer may become unresponsive.
        >>> Do you want to abort the script?
        >>> [ OK ] [Cancel]
        >>>
        >>> besides the very confusing OK/Cancel buttons in FireFox, is there a
        >>> way to tell a javascript to give control back to the agent interface
        >>> for a few moments?[/color]
        >>
        >> You may find this enlightening (and sympathetic too):
        >>
        >> <URL:http://www.fourmilab.c h/fourmilog/archives/2005-08/000568.html>
        >>[/color][/color]

        I did run across this page while trying to figure out what I could do,
        but decided it to be a "hack" approach and try a different way because
        if someone else wants to use the script (as perversely as it sounds)
        that or those someones would need to adjust their agents and that wouldn't
        necessarily have to be FF alone, and since I didn't find any "other"
        obvious possibility I decided to post my question here.

        [...][color=blue][color=green]
        >> Using DOM is commendable, but for this task I think you will find that
        >> good 'ol non-standard 'DOM 0' document.write is hugely faster.
        >>
        >> Excuse me for not testing it! I'll have a go and post back later...[/color]
        >
        > Try this one:
        > - closing tags for tr & td omitted
        > - unused head, foot and caption elements omitted
        > - unneccessary (in this case) tbody omitted[/color]

        I understand the last two :-) but I didn't know tr/th/td end tags were optional until now
        (probably until I or someone else change the document to xhtml...)

        cool, thanks, now that's slim jim ;-)
        [color=blue]
        > function createlist() {
        > var hex="0123456789 abcdef";
        > hex=hex.split(" ");
        > var n = hex.length;
        > var fourth, third, second, first, spezial;
        > var oBody = document.body;
        > var txt=[];
        >
        > for ( first=0; first<n; first++ ) {
        > for (second=0;secon d<n;second++) {
        > txt.push('<tabl e border="1"><tr> <th>Hex<');
        >
        > for ( oHell=0; oHell<n; oHell++){
        > txt.push('<th>' + hex[oHell]);
        > }
        >
        > for ( third=0; third<n; third++) {
        > txt.push('<tr>< td>' + '&#x' + hex[first]
        > + hex[second] + 'n' + hex[third] + ';');
        >
        > for ( fourth=0; fourth<n; fourth++) {
        > txt.push('<td>' + '&#x' + hex[first]
        > + hex[second] + hex[fourth] + hex[third] + ';');
        > }
        > }
        > txt.push('</table>');
        > }
        > }
        > document.write( txt.join(''));
        > }[/color]

        and the message didn't appear! :-D
        oh, I tried to make "smaller" write() "blocks", moving the document.write( )
        into the "for(first" block, in the hope I would see the page grow, but all
        I got was the message again, so I scraped that.
        Thinking about it now, and after having read the fourmilab.ch article, it's
        clearer to me, because wanting to run the script /and/ wanting to display
        the data as it grows, uses all of FFs graphical/script resources.

        Rob, thanks alot for the tip with the write('html')
        instead of DOM createElement. Works great!

        --
        Robi

        Comment

        Working...