I looked at this before in the internal application, where they didn’t care how long it took for the browser to crack the numbers, they just didn’t want them to click the hint.
The key is to break the work into relatively predictable pieces of work (predictable in terms of CPU time) and run them on setInterval, for example:
function doWork(begin, end) { // Some chunk of what your worker function normally does from begin to end if (actualEnd < end) // Nothing left to do clearInterval(Interval); } var Interval = setInterval(doWork, 15);
This prevents the IE prompt from appearing (or Chrome presents the Freeze dialog box). The next step is to add code that allows the user to completely skip it; if the number of jobs is known at the beginning, ask them right away. If not, start processing and after n pieces ask them if they want to make a cheaper function.
There are 2 more options for doing this job:
- See how much work needs to be done, and if it is a lot, pass it to the server (this, unfortunately, means reusing JS on the server, of course, you can use the server engine that runs Javascript to save some coding).
- Use background workers from Google Gears / HTML5
Finally, if you do this work on demand, there may be acceleration opportunities in the work you do - the data can be indexed in advance on the server to make the calculations faster / easier, something like this, but I don’t know what you count.
Chris moschini
source share