05-07-2012 09:52 AM
What rules does the browser (and webworks) use with regards to terminating long executing Javascript code? Is it based on time or some intelligent detection of stray code?
For example, the following code never gets to the second alert in the Blackberry browser (does in desktop browser) and seems to give in after about 10 seconds.
alert('start');
for (var i=0; i<1000000; i++) {
for (var ii=0; ii<1000000;ii++)
}
alert('finish');
TIA
Dave
05-07-2012 10:37 AM
Before the WebKit engine was introduced (BlackBerry 6 device software) there was a 10 second "timeout" on long-running JavaScript at which point it would automatically be stopped by the web engine. This was put in place as a failsafe to protect the battery against runaway JavaScript.
However this behavior changed with WebKit, in which there is no time limit, however it is dependant on memory instead. If the device runs out of memory, the app may crash.
05-07-2012 11:30 AM
Thanks Adam, but we experience the 10 second timeout on a 9360 with OS 7.1
To be honest the 10 seconds isn't a problem to us, I'm just investigating on why we have scripts in our WebWorks app that just seem to terminate unexpectedly and wondering if the OS is behind it.
Dave
05-07-2012 12:56 PM
Dave,
Since its running on 7.1, have you used remote Web Inspector to identify whether there is any runtime error message displayed when the script stops?
05-08-2012 08:59 AM
Adam, when we run the example script above in the browser on a 9360 with OS 7.1 and have the web inspector running we get the following in the console.
JavaScript execution exceeded timeout.
Since hooking up the web inspector to our app we are also seeing lots of this message around areas where we are parsing incoming json data. Once you seem to get it, almost any script after that seems to give the timeout message, even less than 10 seconds.
We will continue to investigate this end, but could do with a bit more clarifcation on the javascript timeout and what would reset the timer e.g. putting in some console.logs in loops etc.
Dave
05-15-2012 08:26 PM
Any update on this? I'm running into the same issue when I build my database (8000+ records inserted) on my apps first run.
05-16-2012 09:39 AM
No updates. I'm looking into this for you now to confirm if there is supposed to be a 10s timeout for long running scripts.
In the meantime, have you considered using Web Workers as an alternative for handling the heavy load JS processing?
05-16-2012 10:14 AM
Okay I just received confirmation (from browser dev team) that the BB7 browser does forcibly terminate long running scripts.
This is important on mobile in order to protect the device (battery / cpu) from "runaway" scripts. Imagine loading a web page that had an infinite loop - it may freeze the browser UI while processing (Web is single threaded by default) and drain the battery. With this failsafe in place, it protects the user from these conditions.
Note: A user-experience decision was made not to display a user prompt, so this stoppage happens silently.
To work around this, I would suggest re-sizing the volume of JS processing you are doing at once, or as I mentioned earlier, consider using Web Workers to offload some of this work from the main UI thread.
05-16-2012 10:19 AM
Cheers for the clarification Adam. We don't actually have long running scripts in our app was just trying to establish the rules on the 'JavaScript Timeout' message. We have actually narrowed it down to a problem with the geolocation watchposition for which we have made a seperate post!
06-26-2012 03:34 AM - edited 06-26-2012 03:35 AM
Hi Adam
We have a problem very similar to what dpicky described.
Is it possible to get the criteria for this failsafe mechanism? Or even disable it?
Even if it is just to confirm that this is the component that's causing our script to throw an exception.
Thanks in advance
DZucker