Update a status object while in a tight loop ?

Discussion in 'Javascript' started by Richard A. DeVenezia, Apr 3, 2004.

  1. A page has a long running computation loop.
    While in the loop I want to update a status object so I know how
    things are going in the computation.

    This is what I have, but it doesn't seem to update my status object.

    Thanks,
    Richard


    <html>
    <head>
    <script type="text/javascript">

    function compute() {

    var status = document.getElementById ('status')
    var result = document.getElementById ('result')

    var j = 0;
    var n = 1e6;
    var p = n/10;

    // tight loop
    for (var i=0;i<n;i++) {
    // if (j++ > p) { j=0; status.value = i; }
    if (j++ > p) { j=0; setTimeout ( function(){status.value=i} , 1) }
    }

    setTimeout ( function(){status.value='Compute'} , 1)

    result.innerHTML = "Computation results " + i

    return false
    }

    </script>
    </head>
    <body>
    <form name='data'>
    N <input name=N type=text value=5 size=4>
    <input id=status type=submit value='Compute'
    onClick="return compute(document.forms.data.N.value)"
    >

    </form>
    <div id=result></div>
    </body>
    </html>
    Richard A. DeVenezia, Apr 3, 2004
    #1
    1. Advertising

  2. Richard A. DeVenezia wrote:
    <snip>
    > // tight loop
    > for (var i=0;i<n;i++) {
    > // if (j++ > p) { j=0; status.value = i; }
    > if (j++ > p) { j=0; setTimeout ( function(){status.value=i} , 1) }


    The setTimeout function is not able to interrupt executing code. No
    execution scheduled within this function will get an opportunity to
    happen until the - compute - function returns (along with the event
    handler that called it).

    <snip>
    > </script>
    > </head>
    > <body>
    > <form name='data'>
    > N <input name=N type=text value=5 size=4>
    > <input id=status type=submit value='Compute'
    > onClick="return compute(document.forms.data.N.value)"

    <snip> ^^^^^^^^^^^^^^^^^^^
    this.form.N.value

    But the - compute - function has no parameters.

    Richard.
    Richard Cornford, Apr 4, 2004
    #2
    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. Steven

    while loop in a while loop

    Steven, Mar 24, 2005, in forum: Java
    Replies:
    5
    Views:
    2,213
    Tim Slattery
    Mar 30, 2005
  2. Joseph

    Nice a tight loop?

    Joseph, Jul 7, 2006, in forum: C Programming
    Replies:
    16
    Views:
    685
    Ian Collins
    Jul 7, 2006
  3. Navaneeth

    Using malloc/free in a tight loop

    Navaneeth, Nov 17, 2010, in forum: C Programming
    Replies:
    9
    Views:
    1,465
    Nobody
    Nov 19, 2010
  4. gry

    performance of tight loop

    gry, Dec 14, 2010, in forum: Python
    Replies:
    8
    Views:
    290
    Peter Otten
    Dec 14, 2010
  5. Isaac Won
    Replies:
    9
    Views:
    349
    Ulrich Eckhardt
    Mar 4, 2013
Loading...

Share This Page