View Single Post
Old June 12th, 2008, 01:43 PM
Old Pedant Old Pedant is offline
Friend of Wrox
Join Date: Jun 2008
Location: Snohomish, WA, USA
Posts: 1,649
Thanks: 3
Thanked 141 Times in 140 Posts

First of all: LOL! I finally realized you thought I meant that you, personally, had to be patient! Not at all; just meant that the *CODE* had to be patient! DOH on me.

Anyway... The reason that Ajax *CAN* work, at all, is that it is *NOT* using JavaScript code for the important part! It is using the XMLHTTP *component* (which in MSIE is a COM component; dunno how it is implemented in FireFox, but clearly not in JS code). So *THAT* component is capable of multithreading. And doing I/O. And releasing control until it receives a response from the server. And, finally, calling *BACK* into your JS code when the answer arrives.

So, yes, the data has already arrived from the server into the XMLHTTP component and the XMLHTTP component has actually already posted an event to the JavaScript code. So as soon as your JS code stops greedily holding onto the single thread--even if it just relinquishes its hold by doing a setTimeout [and yes, even with a zero-time timeout]--then *WHAM*, that event is allowed to actually invoke the JS code and you get the data in your JS function.

So... The secret is indeed multi-threading, just not multi-threading in JavaScript.

Make more sense?