Running CPU Intensive JavaScript Computations in a Web Browser
Julien Lecomte has written up the pattern where you use setTimeout() to keep yielding control back to the main thread so that it can handle browser events.
Completion Callback
-
-
function doSomething (callbackFn [, additional arguments]) {
-
// Initialize a few things here…
-
(function () {
-
// Do a little bit of work here…
-
if (termination condition) {
-
// We are done
-
callbackFn();
-
} else {
-
// Process next chunk
-
setTimeout(arguments.callee, 0);
-
}
-
})();
-
}
-
Progress Callback
-
-
function doSomething (progressFn [, additional arguments]) {
-
// Initialize a few things here…
-
(function () {
-
// Do a little bit of work here…
-
if (continuation condition) {
-
// Inform the application of the progress
-
progressFn(value, total);
-
// Process next chunk
-
setTimeout(arguments.callee, 0);
-
}
-
})();
-
}
-
Of course, the Gears Workerpool does this the right way, giving you the ability to pass messages to another process to get the work done in a secure sandbox.
As a band-aid it could be nice to have an abstraction library that uses WorkerPool if Gears is installed, if not, try to use setTimeout (which is harder to do as you need to cut up the work differently).
Read more on the source site
No comments yet.
feel free to leave a comment
Comment Guidelines: Basic XHTML is allowed (a href, strong, em, code). All line breaks and paragraphs are automatically generated. Off-topic or inappropriate comments will be edited or deleted. Email addresses will never be published. Keep it PG-13 people!
XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong>
All fields marked with " * " are required.

