There is any way to do multithreading in javascript? or else is there any alternate solution to do that?
Multithreading in javascript?
Hi,
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Akkiraju IvaturiPosted Sep 21, 2012, 10:54 PM
Javascript does not support multithreading because the javascript interpreter in the browser is a single thread (AFAIK). Even Google Chrome will not let a single web page's Javacript run concurrently because this would cause massive concurrency issues in existing web pages. All Chrome does is separate multiple components (different tabs, plugins, etcetera) into separate processes, but I can't imagine a single page having more than one Javacript thread.
You can however use, as was suggested, use setTimeout to allow some sort of scheduling and 'fake' concurrency. This causes the browser to regain control of the rendering thread, and start the Javascript code supplied to setTimeout after the given number of milliseconds. This is very useful if you want to allow the viewport (what you see) to refresh while performing operations on it. Just looping through e.g. coordinates and updating an element accordingly will just let you see the start and end positions, and nothing in between.
We use an abstraction library in Javascript that allows us to create processes and threads which are all managed by the same Javascript interpreter. This allows us to run actions in the following manner:
This allows some form of scheduling and fakes parallelism, starting and stopping of threads, etcetera, but it will not be true multithreading. I don't think it will ever be implemented in the language itself, since true multithreading is only useful if the browser can run a single page multithreaded (or even more than one core), and the difficulties there are way larger than the extra possibilities.
[Copied from some source]
If you are using HTML 5, then following link may be interesting to you:
I suggest going through web workers might help you. Just check it in Google.
http://blogs.msdn.com/b/davrous/archive/2011/07/15/introduction-to-the-html5-web-workers-the-javascript-multithreading-approach.aspx