Полезная информация

JavaScript: The Definitive Guide

Previous Chapter 10
Client-Side Program Structure
Next
 

10.7 JavaScript and Threads

Since a web browser can display multiple documents at the same time by using multiple windows and frames, and since any of those documents can contain JavaScript code, it is natural to wonder whether more than one script can be running at the same time.

The answer is no. Although Navigator, like most browsers, is multithreaded, JavaScript in Navigator 2.0 and 3.0 is single-threaded: only one script may run at a time. If a script is running, other scripts will not be able to run until it has finished. This is an implementation-dependent feature of Navigator, not something inherent about client-side JavaScript itself. It could be that we will see multithreaded JavaScript in future releases.

Because JavaScript is not multithreaded, scripts and event handlers should be written so that they do only small amounts of computation and return quickly. If a script or event handler runs for more than about a half second, the delay will potentially be noticeable and annoying to the user. If your script runs for even longer than this (say 3 or more seconds) then the browser may appear to have "locked up" or "frozen" and the user may think that it has crashed. Note that Navigator 2.0 and 3.0 do not display the Stop button while JavaScript code is running, so there is no way for the user to abort a script that is taking a long time (see the subsection below for an exception, however).

If you need to write a computation-intensive script, one technique is to break it up into small chunks and have each chunk do its computation, and then invoke the next chunk through the Window.setTimeout() method. Doing this, even with a 0 millisecond delay, will give Navigator a chance to do any updating it needs to do itself and will also give scripts from other windows a chance to run. In other words, as far as the user is concerned, it will look as if JavaScript is multithreaded. See the reference section for more details on the setTimeout() method of the Window object.

Infinite Loops in JavaScript

JavaScript is one of the few programming languages in which you cannot write an infinite loop! In order to prevent buggy or malicious code from monopolizing the browser and consuming lots of CPU time,[5] the JavaScript interpreter keeps track of how long a script has run for. When a script seems excessively long, Navigator pops up a dialog box that informs you that a script is still running ("Lengthy JavaScript still running. Continue?"), and gives you the choice of continuing it or aborting it.

[5] When malicious code does this intentionally, it is called a "denial-of-service attack".

Execution time is not measured in absolute time for these purposes, but in the number of branches the JavaScript code makes. Every one million branches, Navigator will ask again if you want to continue running it. For example, a very simple loop like for(var i = 0;;i++); will run one million times before Navigator asks you if you want to abort it. More complex loops will run fewer times. The actual time elapsed before Navigator gives you the option of aborting the script depends entirely upon the speed of your computer.


Previous Home Next
Execution of JavaScript Programs Book Index Windows and the JavaScript Name Space

HTML: The Definitive Guide CGI Programming JavaScript: The Definitive Guide Programming Perl WebMaster in a Nutshell