11.12.2012 Views

JavaScript 2.0-The Complete Reference, Second ... - freecodingtutorial

JavaScript 2.0-The Complete Reference, Second ... - freecodingtutorial

JavaScript 2.0-The Complete Reference, Second ... - freecodingtutorial

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

User: Deagol (check<br />

online status) [should be true]<br />

Notice in the preceding script that we go to great lengths to accommodate the fact that the<br />

image may take time to download (either because of slow server processing or a slow network<br />

connection). Since the RPC (image download) may take time, we schedule the callback<br />

function to be run every 50 milliseconds. Each time it runs, readResponse() checks the<br />

complete property of the Image we‘re using for the request. As discussed in Chapter 15, this<br />

property is set by the browser when the image has completed downloading. If complete is<br />

true, the <strong>JavaScript</strong> reads the response encoded in the image‘s height. If complete is false,<br />

the browser needs more time to fetch the image, so readResponse() is scheduled to run again<br />

50 milliseconds in the future.<br />

By far the most common mistake programmers make when implementing two-way<br />

communication techniques in <strong>JavaScript</strong> is forgetting to allow for the possibility that the RPC<br />

takes longer than expected. Paranoid coding is definitely called for in these situations, and<br />

setTimeout() is a useful tool. We‘ll see a more sophisticated callback-based approach in a later<br />

section.<br />

One other noteworthy feature of the previous example is that we only allow for one outstanding<br />

RPC at a time. If a new RPC comes in while we‘re still waiting for one to complete, we cancel<br />

the first and issue the second. This policy simplifies coding a bit, but in truth accommodating<br />

multiple outstanding requests at one time isn‘t much more work. All it takes is carefully<br />

managing three things: references to the images executing each RPC, the functions that should<br />

be called when each RPC completes, and the timers used to periodically check if an RPC has<br />

completed.<br />

Threading<br />

A thread is an execution stream in the operating system. Code executing in a thread can do<br />

exactly one thing at a time; to achieve multiprocessing, an application needs to be multithreaded<br />

(i.e., be able to execute multiple streams of instructions at once).<br />

Almost without exception, <strong>JavaScript</strong> interpreters are single-threaded, and they often share the<br />

browser‘s UI thread. This means that when your <strong>JavaScript</strong> is doing something, no other<br />

<strong>JavaScript</strong> can execute, nor can the browser react to user events such as mouse movement or<br />

button clicks. For this reason, it is never a good idea to ―block‖ your <strong>JavaScript</strong> waiting for some<br />

condition.<br />

For example, instead of registering a timer to check whether the request in the previous<br />

example had completed, we might have done away with the timers and written readResponse()<br />

as<br />

function readResponse()<br />

{

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!