Update a status object while in a tight loop ?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Richard A. DeVenezia

    Update a status object while in a tight loop ?

    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.getEle mentById ('status')
    var result = document.getEle mentById ('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(){stat us.value=i} , 1) }
    }

    setTimeout ( function(){stat us.value='Compu te'} , 1)

    result.innerHTM L = "Computatio n 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(documen t.forms.data.N. value)"[color=blue]
    >[/color]
    </form>
    <div id=result></div>
    </body>
    </html>
  • Richard Cornford

    #2
    Re: Update a status object while in a tight loop ?

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

    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>[color=blue]
    > </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(documen t.forms.data.N. value)"[/color]
    <snip> ^^^^^^^^^^^^^^^ ^^^^
    this.form.N.val ue

    But the - compute - function has no parameters.

    Richard.


    Comment

    Working...