views:

177

answers:

1

Hello Colleagues,

If XMLHttpRequest is used with asynchronous mode, then are the onreadystatechange callbacks for 0,1,2 states called in UI thread only or in the background thread?

And is it possible that this behaviour could be different in different browsers?

Best Regards, Keshav

+1  A: 

Unless you're specifically using background workers (which are browser extensions and not universally implemented yet; i.e. you'd know if you were using them), everything will always run in the same thread. That's how JavaScript works.

Of course, some implementations might optimize things by running JS in a separate thread, but this will always be invisible to you.

Matti Virkkunen
do you mean even if i am using xmlhttprequest in async mode the browser does not use the native threading support for the callbacks?
keshav.veerapaneni
@keshav.veerapaneni: What the browser does it up to the browser. From your (the user's) perspective, everything runs in a single thread.
Matti Virkkunen
thanks for the quick response, so it means if the browser uses single thread for these operations then running a long running background operation in ready state 4 will hang the browser for such browser implementations
keshav.veerapaneni
@keshav: Yes, it will hang. If you want to see the single-threadedness, try running an infinite loop in a click event handler and watch your page freeze.
Matti Virkkunen