Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Script running too long, what to do?

5 views
Skip to first unread message

optimistx

unread,
Jun 30, 2009, 9:13:59 AM6/30/09
to
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?)

Martin Honnen

unread,
Jun 30, 2009, 9:19:56 AM6/30/09
to
optimistx wrote:

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


--

Martin Honnen
http://msmvps.com/blogs/martin_honnen/

Doug Miller

unread,
Jun 30, 2009, 9:30:38 AM6/30/09
to
In article <4a4a0f8f$0$6261$9b53...@news.fv.fi>, "optimistx" <opti...@hotmail.com> wrote:
>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?


Jorge

unread,
Jun 30, 2009, 10:42:32 AM6/30/09
to

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

--
Jorge.

Jorge

unread,
Jun 30, 2009, 10:51:31 AM6/30/09
to

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: this can speed up things a lot (and drain the batteries of your
laptop :-).

--
Jorge.

Richard Cornford

unread,
Jun 30, 2009, 11:09:22 AM6/30/09
to
On Jun 30, 3:51 pm, Jorge wrote:
<snip>

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

Jorge

unread,
Jun 30, 2009, 11:28:10 AM6/30/09
to
On Jun 30, 5:09 pm, Richard Cornford <Rich...@litotes.demon.co.uk>
wrote:
> (...)

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

Google is your friend.

--
Jorge.

Richard Cornford

unread,
Jun 30, 2009, 11:58:10 AM6/30/09
to
On Jun 30, 4:28 pm, Jorge wrote:

> On Jun 30, 5:09 pm, Richard Cornford wrote:
>> (...)
>> Or have you just taken to making stuff up off the top
>> of your head ?
>
> 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.

Message has been deleted

optimistx

unread,
Jun 30, 2009, 1:17:29 PM6/30/09
to

"Martin Honnen" <maho...@yahoo.de> kirjoitti
viestiss�:4a4a10fe$0$32681$9b4e...@newsspool2.arcor-online.net...

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


Dr J R Stockton

unread,
Jun 30, 2009, 3:15:57 PM6/30/09
to
In comp.lang.javascript message <4a4a0f8f$0$6261$9b53...@news.fv.fi>,
Tue, 30 Jun 2009 16:13:59, optimistx <opti...@hotmail.com> posted:

>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 <news:mozilla.support.firefox>

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

--
(c) John Stockton, nr London, UK. ?@merlyn.demon.co.uk Turnpike v6.05 MIME.
Web <URL:http://www.merlyn.demon.co.uk/> - FAQqish topics, acronyms & links;
Astro stuff via astron-1.htm, gravity0.htm ; quotings.htm, pascal.htm, etc.
No Encoding. Quotes before replies. Snip well. Write clearly. Don't Mail News.

0 new messages