No. You cannot separate execution in processes or threads in this way. All engine Javascript will run in a single environment.
There may even be some situations that the browser can do some operations, usually internal things, in a thread separate. But this is his "problem". Don’t count on it.
For the programming of script you should consider that you are running all in one thread, although this is not always true especially if you are using asynchronous Apis, events and setInterval
(is the hint of how to get a result next to what you want).
Each block <script>
is executed sequentially and not in parallel. Even the order that each block appears on the page influences the execution. At most what you can achieve is that a script coming externally by a src
be loaded in parallel, but its execution will be sequential respecting the order in which it appears on the page.
Then the execution order is guaranteed... until you turn into a browser not following the pattern. Guess which browser does not give this guarantee? It is the same as always that is different from others.
It seems that HTML allows this to be specified with <script type="text/javascript" async src="script.js"></script>
but I don’t know what the implementation is like in the browsers. Note that this refers only to the file load (possibly the parse and compilation/JIT as well).
Anyway this is a browser implementation detail and not something specified, something you might consider.
If you want simultaneous run need to use Webworkers, if the browser has this feature available.
As far as I know Javascript is single threaded, these two pieces of code will be run sequentially and not parallel.
– Sergio
It was worth @Sergio . I didn’t know it was single and sequential. I even imagined it to be sequential, but I wasn’t sure. The html world today is very signed, you will know... hehe
– Marcelo Gomes