---restore content starts---
JS Asynchronous load Synchronous loading <script src= "http://baidu.com/script.js" ></script>---> What we usually use is this synchronous loading mode, also known as blocking mode, Prevents subsequent processing of the browser, stops subsequent parsing, and therefore stops subsequent file loading (like), rendering, and code Execution. JS is synchronous execution, because JS may have output document content, modify the dom, redirection and other behaviors, so the default synchronous execution is Safe. The previous general recommendation was to put <script> before the end of the page </body>, so as to minimize this blocking behavior, and first let the page show.Asynchronous loading
There are several ways to load asynchronously, and now we introduce the following two common types of
- Async: Definition and usage of async (is a property of HTML5)
The async attribute specifies that once the script is available, it executes asynchronously.
Example:
Note: The Async attribute is only available for external scripts (only when using the SRC Attribute)
Note: There are several ways to execute external scripts:
- If Async is not used and defer= "defer": the script executes when the page finishes parsing
- If async= "async": The script executes asynchronously relative to the rest of the page (the script will be executed when the page resumes Parsing)
- If neither async nor defer is used: reads and executes the script immediately before the browser continues to parse the page
2. Create a JS script, insert into the dom, and callback after loading, for Example:
functionloadscript (url, Callback) {varScript = document.createelement_x ("script") Script.type= "text/javascript"; if(script.readystate) {//IEScript.onreadystatechange =function(){ if(script.readystate = = "loaded" | |script.readystate= = "complete") {script.onreadystatechange=NULL; callback ();} }; } Else{//others:firefox, Safari, Chrome, and OperaScript.onload =function() {callback ();};} SCRIPT.SRC=url; Document.body.appendChild (script);}
Three states of the promise
Promise I also indefinitely, currently only know that there are three states can be called at different times, after the actual process encountered I will add.
On the sauce, Above.
Three states of JS asynchronous loading and promise