Update a status object while in a tight loop ?

  • Thread starter Richard A. DeVenezia
  • Start date
R

Richard A. DeVenezia

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>
 
R

Richard Cornford

Richard A. DeVenezia wrote:
// 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).

</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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top