Why doesn't this work?

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

    #1

    Why doesn't this work?


    Hello,

    I wrote a script that loops through a list of addresses and does stuff
    with those addresses, and while it is working there isn't anything to
    see. In order to be able to see progress being made, I thought I
    could make a couple of text boxes in a form and have one box show the
    address number it's on and the other show the total number of
    addresses... so basically I want one box to say for example 10, if
    there were 10 addresses and the other box to count from 1 to 10 as it
    processes...

    <script>
    function begin() {
    count=0;
    num=document.ad ds.num.value;
    while (count < num) {
    document.adds.c ount.value=coun t;
    RunSearch();
    count++;
    delay(1000);
    }
    }

    function delay(gap){ /* gap is in millisecs */
    var then,now; then=new Date().getTime( );
    now=then;
    while((now-then)<gap)
    {now=new Date().getTime( );}
    }
    </script>

    <form name=adds method=POST>
    <input type="text" name=count value=>
    of
    <input type="text" name=num value=10>
    <input type="button" onClick="begin( );">
    </form>

    When I execute this, it shows the first box blank, the second box with
    a 10 in it and then after the script is finished it then makes the
    first box a 10. I was hoping to make the first box update as it
    goes, showing a 1, then 2, then 3....

    Is what i'm trying to do not possible or am I just doing something
    wrong?

    Thanks,
    Jeff

  • Lee

    #2
    Re: Why doesn't this work?

    Jeff said:[color=blue]
    >
    >
    >Hello,
    >
    >I wrote a script that loops through a list of addresses and does stuff
    >with those addresses, and while it is working there isn't anything to
    >see. In order to be able to see progress being made, I thought I
    >could make a couple of text boxes in a form and have one box show the
    >address number it's on and the other show the total number of
    >addresses... so basically I want one box to say for example 10, if
    >there were 10 addresses and the other box to count from 1 to 10 as it
    >processes...
    >
    ><script>
    >function begin() {
    > count=0;
    > num=document.ad ds.num.value;
    > while (count < num) {
    > document.adds.c ount.value=coun t;
    > RunSearch();
    > count++;
    > delay(1000);
    > }
    >}
    >
    >function delay(gap){ /* gap is in millisecs */
    >var then,now; then=new Date().getTime( );
    >now=then;
    >while((now-then)<gap)
    >{now=new Date().getTime( );}
    >}
    ></script>
    >
    ><form name=adds method=POST>
    ><input type="text" name=count value=>
    >of
    ><input type="text" name=num value=10>
    ><input type="button" onClick="begin( );">
    ></form>
    >
    >When I execute this, it shows the first box blank, the second box with
    >a 10 in it and then after the script is finished it then makes the
    >first box a 10. I was hoping to make the first box update as it
    >goes, showing a 1, then 2, then 3....
    >
    >Is what i'm trying to do not possible or am I just doing something
    >wrong?[/color]

    You're doing something horribly wrong. You're keeping the CPU
    completely busy, so there is no time for it to update the display
    or process any new input. If you want to create a delay between
    iterations of a loop, use setTimeout() to schedule each successive
    iteration:

    <input type="button" value="begin" onclick="begin( 0)">

    function begin(count) {
    if (count < +document.adds. num.value) { // + converts to number
    document.adds.c ount.value=coun t;
    RunSearch();
    count++;
    setTimeout("beg in("+count+")", 1000);
    }
    }

    Comment

    • Jeff

      #3
      Re: Why doesn't this work?

      Thanks so much...
      You're right that was a really dumb way for me to do the delay. I
      tried your approach, and it worked great.. and fixed some other issues
      I was having too. As you can imagine tying up the CPU the way I was
      messed up a number of things.

      -Jeff

      Comment

      • Dr John Stockton

        #4
        Re: Why doesn't this work?

        JRS: In article <1115910051.483 154.117720@g49g 2000cwa.googleg roups.com>
        , dated Thu, 12 May 2005 08:00:51, seen in news:comp.lang. javascript,
        Jeff <jdport@veritim e.com> posted :

        Please use sensibly helpful Subject lines.
        [color=blue]
        >I wrote a script that loops through a list of addresses and does stuff
        >with those addresses, and while it is working there isn't anything to
        >see.
        > ...
        >function delay(gap){ /* gap is in millisecs */
        >var then,now; then=new Date().getTime( );
        >now=then;
        >while((now-then)<gap)
        >{now=new Date().getTime( );}
        >}[/color]

        [color=blue]
        > ...
        >Is what i'm trying to do not possible or am I just doing something
        >wrong?[/color]

        That is done in <URL:http://www.merlyn.demo n.co.uk/js-tourn.htm> except
        that the task is different. Note the tournament count, at the bottom of
        the middle agitated area.

        Assume we have N items to process; set counter K to 0; do something like
        the following pseudo-code, in which work stops once a second to allow
        update, and restarts almost immediately :-


        function Work() { var D = +new Date() + 1000
        repeat
        DoItem(K)
        K++
        until K>N or +new Date() > D
        write progress
        }

        repeat

        setTimeout("Wor k()", 10)

        until K=N



        However, I now see that js-tourn.htm uses setInterval ... ... see also
        js-pairs.htm

        Your delay is a time-waster; but it could be shorter, along the lines of
        :-

        function delay(gap) { var D = +new Date() + gap
        while (+new Date() > D) {} }

        --
        © John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v4.00 IE 4 ©
        <URL:http://www.jibbering.c om/faq/> JL/RC: FAQ of news:comp.lang. javascript
        <URL:http://www.merlyn.demo n.co.uk/js-index.htm> jscr maths, dates, sources.
        <URL:http://www.merlyn.demo n.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.

        Comment

        • Jimnbigd

          #5
          Re: Why doesn't this work?

          The browser will not update the screen while script code is executing. You
          need to do a setTimeout (or setInterval) to get updated displays to actually
          display. My tests in IE work with a zero time. Other browsers might
          require a positive time; I don't know. Here is an example:

          <html>
          <head>
          <title>javatest .htm</title>

          <script>
          var count=0
          function begin() {
          num=document.ad ds.num.value;
          if (count < num) {
          document.adds.c ount.value=coun t;
          RunSearch();
          count++;
          window.setInter val('begin()',0 );
          }
          }

          function RunSearch()
          {
          var x=""
          var i
          for (i=0;i<500000;i ++)
          {
          x='abc';
          x='xyz';
          }
          }

          </script>

          <form name=adds method=POST>
          <input type="text" name=count value=>
          of
          <input type="text" name=num value=10>
          <input type="button" onClick="begin( );">
          </form>
          </html>


          I defined my own function RunSearch to similate your function that takes a
          while. You would use your own RunSearch function, of course. You do not
          need (nor want) your own delay function, I don't think. In my example
          above, function "begin" calls itself using the setTimeout. Function
          setTimeout returns to the browser so it can update the screen. Note that
          variable "count" is a global -- defined outside of the function, so that its
          value is remember each time function "begin" is called. Does anyone have
          another method?
          ....Jim Lee, Dallas, TX...


          "Jeff" <jdport@veritim e.com> wrote in message
          news:1115910051 .483154.117720@ g49g2000cwa.goo glegroups.com.. .[color=blue]
          >
          > Hello,
          >
          > I wrote a script that loops through a list of addresses and does stuff
          > with those addresses, and while it is working there isn't anything to
          > see. In order to be able to see progress being made, I thought I
          > could make a couple of text boxes in a form and have one box show the
          > address number it's on and the other show the total number of
          > addresses... so basically I want one box to say for example 10, if
          > there were 10 addresses and the other box to count from 1 to 10 as it
          > processes...
          >
          > <script>
          > function begin() {
          > count=0;
          > num=document.ad ds.num.value;
          > while (count < num) {
          > document.adds.c ount.value=coun t;
          > RunSearch();
          > count++;
          > delay(1000);
          > }
          > }
          >
          > function delay(gap){ /* gap is in millisecs */
          > var then,now; then=new Date().getTime( );
          > now=then;
          > while((now-then)<gap)
          > {now=new Date().getTime( );}
          > }
          > </script>
          >
          > <form name=adds method=POST>
          > <input type="text" name=count value=>
          > of
          > <input type="text" name=num value=10>
          > <input type="button" onClick="begin( );">
          > </form>
          >
          > When I execute this, it shows the first box blank, the second box with
          > a 10 in it and then after the script is finished it then makes the
          > first box a 10. I was hoping to make the first box update as it
          > goes, showing a 1, then 2, then 3....
          >
          > Is what i'm trying to do not possible or am I just doing something
          > wrong?
          >
          > Thanks,
          > Jeff
          >[/color]


          Comment

          Working...