Question

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

No correct solution

OTHER TIPS

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/

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

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>

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.

Web workers

You can use Web Workers to create background threads

Web Workers provide a simple means for web content to run scripts in background threads. The worker thread can perform tasks without interfering with the user interface. In addition, they can perform I/O using XMLHttpRequest (although the responseXML and channel attributes are always null). Once created, a worker can send messages to the JavaScript code that created it by posting messages to an event handler specified by that code (and vice versa.) This article provides a detailed introduction to using web workers.

Browser compatibility

Chrome: 4

Firefox (Gecko): 3.5

Internet Explorer: 10.0

Opera: 10.6

Safari (WebKit): 4

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top