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??

3 Answers 3

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?

Sign up to request clarification or add additional context in comments.

1 Comment

+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.
1

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

Comments

0

JavaScript won't execute the code parallel by default; to execute a code in background you need to create webworkers. Currently your code works on a first come, first served basis.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.