Three states of JS asynchronous loading and promise

Source: Internet
Author: User

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

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.