Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Web and WebWorks Development

Reply
New Developer
dpicky
Posts: 57
Registered: ‎08-08-2008
My Device: Not Specified

Javascript termination

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

 

Please use plain text.
Administrator
astanley
Posts: 1,382
Registered: ‎07-02-2009
My Device: BlackBerry Bold 9900
My Carrier: Bell

Re: Javascript termination

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.

Follow me on Twitter: @n_adam_stanley
-------------------------------------------------------------------------------------------------------------------------
Your app doesn't work? Use BlackBerry remote web inspector to find out why.
Please use plain text.
New Developer
dpicky
Posts: 57
Registered: ‎08-08-2008
My Device: Not Specified

Re: Javascript termination

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

Please use plain text.
Administrator
astanley
Posts: 1,382
Registered: ‎07-02-2009
My Device: BlackBerry Bold 9900
My Carrier: Bell

Re: Javascript termination

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?

 

 

Follow me on Twitter: @n_adam_stanley
-------------------------------------------------------------------------------------------------------------------------
Your app doesn't work? Use BlackBerry remote web inspector to find out why.
Please use plain text.
New Developer
dpicky
Posts: 57
Registered: ‎08-08-2008
My Device: Not Specified

Re: Javascript termination

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

Please use plain text.
Developer
bnolan001
Posts: 127
Registered: ‎02-19-2011
My Device: Bold 9930 and PlayBook

Re: Javascript termination

Any update on this?  I'm running into the same issue when I build my database (8000+ records inserted) on my apps first run. 

"Not to know what happened before you were born is to be forever a child." Marcus Tullius Cicero
Please use plain text.
Administrator
astanley
Posts: 1,382
Registered: ‎07-02-2009
My Device: BlackBerry Bold 9900
My Carrier: Bell

Re: Javascript termination

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?

Follow me on Twitter: @n_adam_stanley
-------------------------------------------------------------------------------------------------------------------------
Your app doesn't work? Use BlackBerry remote web inspector to find out why.
Please use plain text.
Administrator
astanley
Posts: 1,382
Registered: ‎07-02-2009
My Device: BlackBerry Bold 9900
My Carrier: Bell

Re: Javascript termination

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.

 

Follow me on Twitter: @n_adam_stanley
-------------------------------------------------------------------------------------------------------------------------
Your app doesn't work? Use BlackBerry remote web inspector to find out why.
Please use plain text.
New Developer
dpicky
Posts: 57
Registered: ‎08-08-2008
My Device: Not Specified

Re: Javascript termination

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!

Please use plain text.
New Contributor
dzucker
Posts: 4
Registered: ‎04-24-2011
My Device: Torch 9800
My Carrier: Vodafone

Re: Javascript termination

[ Edited ]

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

Please use plain text.