Does one script start running only after another or at the same time?

Asked

Viewed 665 times

1

If I load two scripts by html the second will start running only after the first one has loaded or at the same time as the first?

2 answers

1


Your question may have several interpretations. From what I understand you want to know if including 2 scripts with the tag script, it will run one after the other. Abstracting, the answer would be yes. Several factors may cause the second to run first. An example of this is if you put the attribute async on the tag script, then the browser that will manage which one runs first.

  • This means that html is synchronous by default for everything?

  • 1

    @Viniciusputtimorais yes.

1

Scripts will be executed in the order they are loaded. Javascript by default is synchronous.

Note that this does not mean that all the logic of a script will be executed immediately when it is loaded. If you do something like:

<script type="text/javascript">
    $().ready(function () {
        console.log("Script 1");
    })
</script>
<script type="text/javascript">
    console.log("Script 2");
</script>
<!---->

You can see in the console first "script 2", then "script 1". The first script actually ran first, but the part where it writes on the console is on hold until the DOM is ready.

Note that some people may say that Ajax, setInterval, setTimeout etc. allow asynchronous programming. If you only work with these things you will still only use a single thread for your code. To run threads simultaneously you will need to Web Workers.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.