3

As is evident when the document gets loaded in the client browser,

$(function(){
some code here
});

takes over.

Say I have two JavaScript files main.js and style.js

main.js is for the functionality and style.js for some hypothetical styling when the page loads. I want both the files. I include them in my index.html first style.js then main.js both of them start with:

$(function(){
    some code here
    });

My question is, what is the order of execution of document.ready is it that main.js and style.js start doing things parallely or is it sequential, once style.js has finished what it should do then main.js takes over??

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
user993563
  • 18,601
  • 10
  • 42
  • 55

3 Answers3

4

It is sequential. There is no parallel processing in javascript. They will be called in the order you included your scripts on the page.

This is a good answer too: Can you have multiple $(document).ready(function(){ ... }); sections?

Community
  • 1
  • 1
Alexandru Petrescu
  • 3,449
  • 2
  • 23
  • 23
  • 1
    +1. It would be more precise to say "called in order scripts files are executed". Depending on presence of "defer" attribute order in which script tags are and load order may not be the same. – Alexei Levenkov Apr 06 '12 at 07:06
1

Well you can have multiple document.ready but that affects the readability of the code. More has been explained here

whatf
  • 6,378
  • 14
  • 49
  • 78
0

Javascript wont execute the code parallel by default, to execute a code in background you need to create webworkers. Currently your code work on first come first basis.

Sandeep Manne
  • 6,030
  • 5
  • 39
  • 55