Script running too long, what to do?

O

optimistx

I am trying to write a calculation intensive simulator, and have done some
quick and dirty tests with javascript. A calculation takes some minutes.

Ie, ff,chrome complain that my script is running too long, a window appears,
and the user can select to continue calculation.

Outputting something during the running script to a div with innerHTML did
not help (the output becomes visible much later).

How to prevent the complaints? (or, how to let the browsers think that there
are several shortrunning scripts instead?)
 
M

Martin Honnen

optimistx said:
How to prevent the complaints? (or, how to let the browsers think that there
are several shortrunning scripts instead?)

You need to break up the computation and start each part with setTimeout.
 
D

Doug Miller

I am trying to write a calculation intensive simulator, and have done some
quick and dirty tests with javascript. A calculation takes some minutes.

Ie, ff,chrome complain that my script is running too long, a window appears,
and the user can select to continue calculation. [...]
How to prevent the complaints?

Speed up the calculation, obviously. The first thing to examine is your
algorithm: is there a more efficient method of accomplishing the same task?

Next examine your implementation: have you implemented this method in the most
efficient way possible?
 
J

Jorge

optimistx said:
I am trying to write a calculation intensive simulator, and have done some
quick and dirty tests with javascript. A calculation takes some minutes.

Ie, ff,chrome complain that my script is running too long, a window appears,
and the user can select to continue calculation.

Outputting something during the running script to a div with innerHTML did
not help (the output becomes visible much later).

How to prevent the complaints? (or, how to let the browsers think that there
are several shortrunning scripts instead?)

function longCalculation () {
...
do some work during no more than a few seconds.
...
return finished;
}

(function loopUntilDone () {
if (!longCalculation()) {
//continue calculating
setTimeout(loopUntilDone, 0);

} else {
//calculation done

}
})();

Or use a web worker:

See:
http://www.whatwg.org/specs/web-workers/current-work/
http://code.google.com/apis/gears/sample.html
http://code.google.com/apis/gears/samples/hello_world_workerpool/hello_world_workerpool.html
 
J

Jorge

R

Richard Cornford

On Jun 30, 3:51 pm, Jorge wrote:
Note that each webworker (not available in IEs) runs in a
separate thread and is automatically assigned to a different
core in a multicore CPU:
<snip>

That makes no sense at all. If all other available cores are already
running at 100% and the one running the browser/JS engine is idling
while it waits for user input it makes much more sense to use the same
core as the existing JS engine, not a "different" one. Better to leave
the assignment of CPU cores to the OS.

Or have you just taken to making stuff up off the top of your head?

Richard.
 
R

Richard Cornford

Google is your friend.

What search term would you recommend as best able to tell me whether
"runs in a separate thread and is automatically assigned to a
different core in a multicore CPU" is the stupidest design decision
ever or a fiction that originated with you?

Richard.
 
J

Jorge

(...)
What search term would you recommend as best able to tell me whether
"runs in a  separate thread and is automatically assigned to a
different core in a multicore CPU" is the stupidest design decision
ever or a fiction that originated with you?

Do you want a link ? You mean ?
 
O

optimistx

You need to break up the computation and start each part with setTimeout.

Thank you very much. In a quick test it seems to work now. And thanks for
all who replied.
 
D

Dr J R Stockton

In comp.lang.javascript message said:
I am trying to write a calculation intensive simulator, and have done some
quick and dirty tests with javascript. A calculation takes some minutes.

Ie, ff,chrome complain that my script is running too long, a window appears,
and the user can select to continue calculation.

With, in FF 3.0.11, a checkbox for not asking again. To restore
normality after using that, use about:config in the address bar and
restore dom.max_script_run_time (to 10).

IMHO, that could be a candidate for FAQ notes miscellanea.

I have not yet seen how to re-enable a suppressed dragon-warning.
Outputting something during the running script to a div with innerHTML did
not help (the output becomes visible much later).

Browser-dependent, IIRC; try Opera, Safari. There might be an option in
FF about:config; you could ask in said:
How to prevent the complaints? (or, how to let the browsers think that there
are several shortrunning scripts instead?)

Or, as has been said, use setTimeOut.
 

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

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top