please dont rip this site

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 =index.html 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

file: /Techref/language/java/script/definitive/ch10_07.htm, 6KB, , updated: 2019/10/14 15:00, local time: 2024/11/15 20:58,
TOP NEW HELP FIND: 
3.133.109.121:LOG IN

 ©2024 These pages are served without commercial sponsorship. (No popup ads, etc...).Bandwidth abuse increases hosting cost forcing sponsorship or shutdown. This server aggressively defends against automated copying for any reason including offline viewing, duplication, etc... Please respect this requirement and DO NOT RIP THIS SITE. Questions?
Please DO link to this page! Digg it! / MAKE!

<A HREF="http://massmind.ecomorder.com/techref/language/java/script/definitive/ch10_07.htm"> [Chapter 10] 10.7 JavaScript and Threads</A>

After you find an appropriate page, you are invited to your to this massmind site! (posts will be visible only to you before review) Just type a nice message (short messages are blocked as spam) in the box and press the Post button. (HTML welcomed, but not the <A tag: Instead, use the link box to link to another page. A tutorial is available Members can login to post directly, become page editors, and be credited for their posts.


Link? Put it here: 
if you want a response, please enter your email address: 
Attn spammers: All posts are reviewed before being made visible to anyone other than the poster.
Did you find what you needed?

 

Welcome to ecomorder.com!

 

Welcome to massmind.ecomorder.com!

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

  .