views:

123

answers:

4

Hi, we are developing a web application using GWT in the front end.

In GWT we make calls to the server by appending javascript code as stated below:

    public native static void call(int requestId, String url, ICall handler) /*-{
   var callback = "callback" + requestId;

   //Create a script element.
   var script = document.createElement("script");
   script.setAttribute("src", url+callback);
   script.setAttribute("type", "text/javascript");
   script.setAttribute("async", "true");

   //Define the callback function on the window object.
   window[callback] = function(response) {
     [email protected]::handleResponse(Ljava/lang/String;)(response);
   } 

   //Attach the script element to the document body.
   document.body.appendChild(script);
  }-*/;

Some calls take a minute to complete and some others just a couple of seconds. If we make multiple calls at the same time all of them are executed in parallel . This means that a call that ends in 2 seconds does not have to wait until a call that lasts a minute finish. This is true in Chrome and Safari. However Firefox waits until the first invoked function finishes to start the other functions.

Why does Firefox waits until a javascript function is finished to start another function? How to fix this?

Thanks

A: 

You may have to explicitly thread the processes to make Firefox run them in parallel (Firefox hasn't had a recent release, so their support for parallel may be weaker than the more recently updated chrome and safari). It may also be that Firefox has some qualm about accessing an object from multiple methods at the same time.

Any reason why the code is all commented out?

Rafe Kettler
Code is commented out because that's a standard way of including Javascript in Java GWT code - to keep Java compiler happy.
Domchi
Thanks Rafe. The code is between /*-{ and }-*/ because GWT uses Java but lets you include native javascript by surrounding it by /*-{ and }-*/I'll take a look at explicitly thread each process.
Miguel
That explains a lot. I was about to have a "well there's your problem!" moment.
Rafe Kettler
Not sure what you mean by Firefox not having a recent release. Firefox 3.6 was released on January 21 (about six months ago).Additionally, no browser runs JavaScript in parallel unless you use web workers, which is a new spec part of HTML5.
sdwilsh
@sdwilsh: Sorry, I was speculating on Chrome and Safari (never worked with them, so idk about what they do and don't do). By release I meant major release, which Firefox hasn't had since 3 came out in June 2008 (2 years ago), though they do have a 4 beta out now.
Rafe Kettler
A: 

JavaScript on the web is single threaded unless you use worker threads (around since Firefox 3.5). This is just how the web is designed to work, and all browsers are going to do things the same way.

sdwilsh
You can also kind-of fake worker theads using setTimeout.
Ben Rowe
Yeah, but I don't think that'd help in this case. If the script is already taking a minute to run, it's still going to lock up your web page for a bit.
sdwilsh
+1  A: 

You can use HTML5 with multithreaded JavaScript through web workers. First check if your browser supports this.

function supports_web_workers() {
  return !!window.Worker;
}

Then start using multithreading.

   // thread1
   var myBread = new Worker('bread.js');

   // thread2
   var myButter = new Worker('butter.js');

I never tried this myself so correct me if I'm wrong.

Stephan Kristyn
I'll have to research this and see how it works...
Rafe Kettler
Thanks Stephan! I'll give it a try.
Miguel
I would be interested in your results, Miguel.
Stephan Kristyn
A: 

Your question is a bit incorrect cause you don't use xhr as you may think but script injections (jsonp). In this case firefox will download the files in parallel but execute the file in the order the files have submitted to the DOM.

eskimoblood