Multiple Threads and single threads of javascript;

Source: Internet
Author: User

Multiple Threads and single threads of javascript;
In javascript, user-defined functions are run in a single thread, but after the rendering part is handed over to the browser, it is run in multiple threads. Other sequences are uncontrollable.
For example:

 
 
  1. Include = function (jsurl ){
  2. If (jsurl = null | typeof (jsurl )! = 'String') return;
  3. Var js = document. createElement ('script ');
  4. Js. type = 'text/javascript ';
  5. Js. charset = 'utf-8 ';
  6. Js. src = jsurl;
  7. Var heads = document. getElementsByTagName ('head ');
  8. Heads [0]. appendChild (js );
  9. // Implement 2
  10. Document. write ('<script type = "text/javascript" src = "' + jsurl + '"> </script> ');
  11. Console.info ("include" + jsurl + "finished ");
  12. }
The code is completed in two ways: Insert a script node, and dynamically write a node through the document;

If the first implementation is used at this time, when multiple js files are added, the head will be inserted at the same time. The loading of these files is controlled by multiple threads in the browser; the loading order cannot be guaranteed, {for example, first include (". js "), in include (" B. js "), B uses the object defined by}
It is not loaded first if it is included first (a cannot be guaranteed to be loaded first ). therefore, if the file to be loaded is dependent at this time, the variable is undefined. It is found through testing. even if it is loaded first, there will be undefined conditions in the future. It is estimated that this is caused by multi‑thread synchronization;

For the second method, you can ensure that the generated script header is consistent with the effect of directly writing the script in html, and the loading sequence is ensured;

Taking this as an example, we can guess that other paintors have the same effect. That is, not necessarily the first append element. Of course, this time difference is very small, and the eyes on the interface cannot find it at all;

Related Article

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.