Why does Firefox waits until a javascript function is finished to start another function?
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) {
handler.@com.xxx.yyy.gui.net.ICall::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
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.
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?
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.
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.
精彩评论