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.

9 comments

Post a comment
  1. Todd A. Fisher wrote on July 1st, 2009 at 11:47 am:

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

    Reply

  2. Ben Turner wrote on July 1st, 2009 at 2:35 pm:

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

    Reply

  3. Anon wrote on July 1st, 2009 at 4:35 pm:

    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

    Reply

  4. Jeff Walden wrote on July 1st, 2009 at 7:59 pm:

    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.

    Reply

  5. Martin wrote on July 8th, 2009 at 4:42 am:

    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

    Reply

  6. Martin wrote on July 8th, 2009 at 11:22 am:

    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.

    Reply

  7. Trackback from Tech, Web, How to, Internet, Computer, Free Software, Tips, Make Money Online with AhTim on July 12th, 2009 at 7:11 pm:

    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…

    Reply

  8. Trackback from Tech, Web, How to, Internet, Computer, Free Software, Tips, Make Money Online with AhTim on July 12th, 2009 at 7:11 pm:

    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…

    Reply

  9. Pingback from Mozilla Hacks: Synchroniczne żądania XHR w Firefoksie 3.5 « marcoos.techblog on July 28th, 2009 at 5:00 am:

    [...] 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 [...]

    Reply

Add your comment