[Javascript] Javascript and threads

Nick Fitzsimons nick at nickfitz.co.uk
Mon May 28 09:23:03 CDT 2007


On 28 May 2007, at 15:07:09, twlewis at SoftHome.net wrote:

> Can threads be implemented in Javascript (such as nsIThread)?
>
> Thanks for any help or websites.
> Tim

No. A browser runs JavaScript in a single thread. If you want to have  
several independent processes running apparently simultaneously,  
you'll have to implement some kind of time-slicing mechanism using  
setTimeout or setInterval:

<http://developer.mozilla.org/en/docs/DOM:window.setTimeout>
<http://developer.mozilla.org/en/docs/DOM:window.setInterval>

If you're doing something like multiple animated elements, which is a  
common reason for wanting thread-like capabilities in JavaScript,  
then you might want to consider taking advantage of one of the many  
JS libraries out there which have done all the hard work for you. I  
can recommend the Yahoo! User Interface Library:

<http://developer.yahoo.com/yui/>

Regards,

Nick.
-- 
Nick Fitzsimons
http://www.nickfitz.co.uk/






More information about the Javascript mailing list