When can we use async?

Async script Download is what I'm learning right now. I have a question about asyncattr in a script tag - <script async src="...">.

here is some information, http://www.w3schools.com/tags/att_script_async.asp , and my question is - If available, it indicates that the script will be executed asynchronously as soon as it is available. .

Question: If I have 2 scripts:

<script async src="url1.js"><script> // 200kb
<script async src="url2.js"><script> // 2kb

and the second scriptshould be executed AFTER the first (it uses some Objectsor functions) - will it be after loading the page? will the script first run first after loading, and then the second or not?

From the bold line above - as I understand it, the faster it loads script- the faster it will be executed, and in our example we will have errors, and the first scriptone was not loaded and executed but the second one is already trying to get it. (and when can we use asyncscript loading? only when our scripts are not independent of each other?)

ps the lazy loading scriptonly way to get the correct script execution order in this case is to prevent errors?

+4
source share
3 answers

Without an attribute, the asyncbrowser will stop processing the page and first load the script link; after loading and executing the script, it will continue processing the next tag on the page. This has two consequences:

  • , HTML, .
  • , . , .

async, , script, script. script, undefined, - . , async , , .

, async , . : window.onload - ; , async scripts.

+2

url2.js url1.js, script, ( ), , url1.js url2. JS.

async :

  • url1.js , url2.js . , url1 url2.
  • url2.js , url1.js . , url2 url1, url2.

, url1.js , url2.js , , .

, , , .

+1

, .

Script -

, JavaScript , HTML. JavaScript JS- , JS.

I suggest you use CallBack features where possible to avoid the above errors. Here is a good place to start learning about CallBack features and how to implement them.

0
source

Source: https://habr.com/ru/post/1616033/


All Articles