synchronous XHR requests in Firefox 3.5

This post is from Doug Turner who has previous written about Geolocation. Doug works on Mozilla’s mobile project.

XMLHttpRequests (XHR) can be either synchronous or asynchronous. Although most people use asynchronous requests there are instances where you might want to use a synchronous request. That is, wait until the XMLHttpRequest call completes to continue executing JavaScript. In Firefox 3 and earlier the browser would still fire timer events and respond to input events during a synchronous XHR request. In Firefox 3.5 and later input events such as mouse moves and timeouts will be suspended until the synchronous request completes. This allows the synchronous request to block.

For example:

function hello() {
     alert(“hello”);
}

setTimeout(hello, 20);

var req = new XMLHttpRequest();
req.open('GET', 'http://www.mozilla.org/', false);
req.send(null);

Prior to Firefox 3.5, it was impossible to determine if the “hello” function would be invoked during or after the XHR request. This led to all sorts of timing issues in web applications that used synchronous XHR requests.

The solution to this problem has been to delay input events and timeouts until after “req.send” returns.

For more information see the two bugs on the issue.


10 comments

  1. Todd A. Fisher

    Does this mean using a synchronous XHR request in a Worker would only block the Worker thread?

    July 1st, 2009 at 11:47

  2. Ben Turner

    Yes, sync XHR in a Worker will block that Worker’s script only.

    July 1st, 2009 at 14:35

  3. Anon

    Could this also explain why mouse wheel scrolling is so slow with sites that use a lot of sync XHR?? I noticed this after updating to FF 3.5

    July 1st, 2009 at 16:35

  4. Jeff Walden

    Todd: that’s correct.

    Outside of worker threads, synchronous XHR is the spawn of Satan. What happens if the user’s network is particularly slow? You could hang the app for twenty or thirty seconds, even longer if you’re really unlucky. Use async and don’t rely on network speed; it doesn’t matter how reasonable you think it is to assume a speedy connection, you will eventually be wrong, and horribly so.

    July 1st, 2009 at 19:59

  5. Martin

    I’m still encountering this problem,

    it seems mouse events are still fired to the flash plugin during a sync XHR request.
    I’m using ExternalInterface to start a js sync XHR Request. While the request is running the flash UI still receives and processes mouse events, even in FF 3.5 which leads to the strange situation that my flash content during this time is somehow multithreaded and more than one actionscript block is executing at once.

    IE and Safari correctly ‘freeze’ the flash content correctly on the contrary

    July 8th, 2009 at 04:42

  6. Martin

    it seems mouse events are still fired to the flash plugin during a sync XHR request.
    I’m using ExternalInterface to start a js sync XHR request. While the request is running the flash UI still receives and processes mouse events, even in FF 3.5. This leads to the strange situation that my flash content is somehow multithreaded during the sync request and more than one actionscript block is executed at once.

    IE and Safari correctly ‘freeze’ the plugin on the contrary.

    July 8th, 2009 at 11:22

  7. Why Your Firefox 3.5 is Slower?…

    tweetmeme_url = ”;tweetmeme_source = ‘tim_un’;
    We all know Firefox 3.5 is fast web browser. Some user experience slow Firefox 3.5 startup after upgraded. Do you face the same problem?
    Let me tell you why mine faster and yo…

    July 12th, 2009 at 19:11

  8. Why Your Firefox 3.5 is Slower?…

    tweetmeme_url = ”;tweetmeme_source = ‘tim_un’;
    We all know Firefox 3.5 is fast web browser. Some user experience slow Firefox 3.5 startup after upgraded. Do you face the same problem?
    Let me tell you why mine faster and yo…

    July 12th, 2009 at 19:11

  9. […] serii tłumaczeń artykułów z bloga Mozilla Hacks, przedstawiam dzisiaj tłumaczenie artykułu Synchronous XHR requests in Firefox 3.5 autorstwa Douga Turnera. Doug pracuje w Mozilli nad projektem […]

    July 28th, 2009 at 05:00

  10. skill-guru

    I do not think this is the best approach. I had been facing same issues and this what O have done to support synchronous calls.
    http://www.skill-guru.com/blog/2011/01/26/synchronous-calls-with-rest-service/

    January 26th, 2011 at 12:05

Comments are closed for this article.