Tech Off Thread

14 posts

Loop Lock

Back to Forum: Tech Off
  • User profile image
    JScript

    What is the technical reason for loop statements locking output until they finish?

    I ask because the Windows Script Host displays incrimental output from a loop, while Internet Explorer can't (or just doesn't)...

    For example:

    <body>
    <input id="output" value="100000">
    <button onclick="decrement(Number(output.value))">decrement</button>
    <script type="text/JScript">
    function decrement(n)
    {
     while(n--)
      output.value = n;
    }
    </script>
    </body>


    The browser locks up until the loop completes, and then outputs "0" (rather than showing the count-down).

    I realize that window.setTimeout() can be used to call a recursive function, by the way; I'd just like to know what's going on with the loop...

  • User profile image
    Cairo


    <script language="javascript">
    </script>


  • User profile image
    JScript

    Sorry, but that doesn't explain much (how about "text/ecmascript").

    Back to the question...

  • User profile image
    figuerres

    JScript wrote:

    What is the technical reason for loop statements locking output until they finish?

    I ask because the Windows Script Host displays incrimental output from a loop, while Internet Explorer can't (or just doesn't)...

    For example:

    <body>
    <input id="output" value="100000">
    <button onclick="decrement(Number(output.value))">decrement</button>
    <script type="text/JScript">
    function decrement(n)
    {
     while(n--)
      output.value = n;
    }
    </script>
    </body>


    The browser locks up until the loop completes, and then outputs "0" (rather than showing the count-down).

    I realize that window.setTimeout() can be used to call a recursive function, by the way; I'd just like to know what's going on with the loop...



    in IE the rendering engine is wating for the chnages to finish so it can render the page.

    thats all.

  • User profile image
    JScript

    Is there an advantage to that?  It would seem more useful to get incrimental feedback (all things being equal)...

  • User profile image
    figuerres

    JScript wrote:
    Is there an advantage to that?  It would seem more useful to get incrimental feedback (all things being equal)...


    well step back and think about that...

    html was built for showing a page of content.

    a "document"

    then everyone said "how boring" so we started creating "dynamic html" which is really a kluge in the end.

    for example you have one element your <input /> tag

    it has an attribute value

    you have a function that modifies that.
    it does so in a loop.

    now any decent compiler with optimizing would see a dummy loop and reduce your function to a constant assignment and you would get the same result w/o the wait.

    thats if you let say a C compiler do full opt.

    now the html engine has to sync events and display.

    as your loop has two operations there is no place for the loop to stop and let the display refresh.

    so the effect is you lock the display then draw the result. any values in the middle may have been drawn to the buffer but never seen.

    as you said you can use a timer to create a count-down.

    that logic applies to any app that draws a screen.

  • User profile image
    JScript

    figuerres wrote:
    that logic applies to any app that draws a screen.


    Well, I'd assumed that other applications had a way of overcoming this (such as when updating a progress bar).  Maybe they all use timers... automation seems to be an exception, somehow, because the WSH does indeed show results for each iteration of a loop statement.

    Thanks for the info. Smiley

  • User profile image
    figuerres

    JScript wrote:

    figuerres wrote: that logic applies to any app that draws a screen.


    Well, I'd assumed that other applications had a way of overcoming this (such as when updating a progress bar).  Maybe they all use timers... automation seems to be an exception, somehow, because the WSH does indeed show results for each iteration of a loop statement.

    Thanks for the info. Smiley


    well with a 1.x or 2.x ghz cpu how long does it take to count up from 0 to one million?
    0.000???? of a second??

    so you have to slow down somehow... to ever see it.

    when you say wsh is that console output?

    then it's print line crlf
    so you have lines of text.
    or perhaps you mean some other use of script host app??

  • User profile image
    JScript

    Yeah, take a similar operation, for example (*.wsf file):

    <job>
    <script language="JScript">
     var ie = WScript.CreateObject("InternetExplorer.Application");
     ie.Navigate("about:blank");
     var d = ie.Document;
     d.open();
     d.write("<input id=\"output\" value=\"5000\">");
     d.close();
     ie.Visible = 1;

     var o = d.parentWindow.output;
     (function(n)
     {
      while(n--)
       if(typeof ie.Busy != "unknown")
        o.value = n;
     })(Number(o.value));
    </script>
    </job>

    I guess its slower than a "locked loop", after all... so the host may be doing the 'timer thing' automatically. Wink

  • User profile image
    figuerres

    JScript wrote:

    Yeah, take a similar operation, for example (*.wsf file):

    <job>
    <script language="JScript">
     var ie = WScript.CreateObject("InternetExplorer.Application");
     ie.Navigate("about:blank");
     var d = ie.Document;
     d.open();
     d.write("<input id=\"output\" value=\"5000\">");
     d.close();
     ie.Visible = 1;

     var o = d.parentWindow.output;
     (function(n)
     {
      while(n--)
       if(typeof ie.Busy != "unknown")
        o.value = n;
     })(Number(o.value));
    </script>
    </job>

    I guess its slower than a "locked loop", after all... so the host may be doing the 'timer thing' automatically. Wink



    very different code... esp:
       if(typeof ie.Busy != "unknown")
        o.value = n;

    you now have a sync function and you are also not updating the value as often.
    you only move n to o.value when the test of the ie state is readable.
    so in effect n keeps chnaging and value only updates when ie is ready to take a new value.
    thats a *LOT* less often.

    the effect is like a timer delay.

  • User profile image
    JScript

    I see (the conditional isn't technically necessary for output; it just prevents an exception, if the browser is closed during the loop); the speed seems about the same either way.

    So setTimeout is like a 'sync function' (as long as there's an interruption of some sort, the result is apparent)...

  • User profile image
    JScript

    OK, so it isn't actually the host, but the scope of the function (or whatever)... since the same sort of thing works from IE, itself:

    <body>
    <script language="Jscript">
     var ie = new ActiveXObject("InternetExplorer.Application");
     ie.Navigate("about:blank");
     var d = ie.Document;
     d.open();
     d.write("<input id=\"output\" value=\"5000\">");
     d.close();

     var o = d.parentWindow.output;
     function ieLoop(n)
     {
       ie.Visible = 1;
        while(n--)
           o.value = n;
     }
    </script>
    <button onclick="ieLoop(Number(o.value))">ieLoop</button>
    </body>

    Oops! Anyway, thanks for the clue... :o

  • User profile image
    DarkByte

    Basicly, what happens is that your loop runs on the UI thread, and i dont believe there is a way to create a thread in JS.

    so, as long as your loop runs steadily, the drawing events are retained.

    There might be solutions. Now, i can't validate what i'm suggesting but i'm sure other folks will put you back on track.

    1) There might be some kind of paint function .. something like output.redraw or output.refresh, etc etc.

    2) If not, its possible (althought im not sure) that there so kind of "yield" or "doevents" .. something like that.

    Its all just theorical since i havent fiddled with JS for a long time.

  • User profile image
    JScript

    Yeah, I just wanted to get an idea of what was happening there and why.  The explanations have helped.

Comments closed

Comments have been closed since this content was published more than 30 days ago, but if you'd like to continue the conversation, please create a new thread in our Forums, or Contact Us and let us know.