views:

351

answers:

4

Is there any idea (library or methodology) to create multithreaded apps in JavaScript?

+3  A: 

JavaScript doesn’t really have multi-threading capabilities , and there’s nothing a JavaScript programmer can do to change that.

However, what we can do is simulate multi-threading. Please go through this article also.

And there is a PDF link on JavaScript Multithread Framework for Asynchronous Processing thesis

pramodc84
+5  A: 

The closest you're gonna get is web workers (only in FF 3.5 / HTML5). Check it out - http://www.whatwg.org/specs/web-workers/current-work/

Dan Beam
John Resig talks about these too: http://ejohn.org/blog/web-workers/. they've even made JavaScript ray tracers, lol.
Dan Beam
A: 

JavaScript is a dynamic programming language and can be used for many different things. If it's not used from within the browser you can fully rely on things like multiple threads, event loops etc. You should check out node.js.

The browser unfortunately only gives you a strict set of features of the language.

Luca Matteis
+1  A: 

Another simulation of threads I found quite stable is to use an image. Which seems to be loaded in another thread in the browser(?). However your callback javascript will run always sequentially.

Here below it loads 500 of them.

<html>
<head>
   <title>so</title>
    <style></style>
</head>
<script>
    function callBack(img){
        var i = 0, img, res = document.getElementById('res'),
            fn = function(cnt){
                var img = document.createElement('img');
                img.onerror = function(ev){
                    res.innerHTML += cnt + ', ';
                    document.body.removeChild(img);
                };
                img.src = 'javascript:void(0)';
                document.body.appendChild(img);
            };
        do{
            fn(i++);
        }while(i<500);
    }
</script>
<body onload="callBack()">
    <div id="res"></div>
</body>
</html>
Mic