国产xxxx99真实实拍_久久不雅视频_高清韩国a级特黄毛片_嗯老师别我我受不了了小说

資訊專欄INFORMATION COLUMN

Five minutes to understand async and defer

Chiclaim / 823人閱讀

摘要:,,

Script tag

When we want to insert a script into a web page, the standard way is to use the script tag(i.e.

hello world

The console output:

script1 is running at 1496747869008
script1.js:4 script1 element null
script1.js:8 script1 finishes running at 1496747870008
script2.js:2 script2 is running at 1496747870009
script2.js:4 script2 element null
script2.js:8 script2 finishes running at 1496747872009

Conclusion:

When we open the html in the browser, we can notice the delay of page load. The page goes blank before it renders correctly. This is due to the fact that the running of the two scripts blocks the DOM rendering.

When scripts are running, they are not able to fetch the DOM element (i.e. document.getElementById("load-experiment") being null). This is because scripts are run before DOM is rendered.

Scripts themselves are blocking each other. They are run in the order specified in the html. Script2 is run after script1 finishes running.

Put script tags at the bottom of body

This is the suggestion from the Rule 6 of the book High Performance Web Sites.



    
         test js tag async and defer attributes
    
    
        

hello world

The console output:

script1 is running at 1496751597679
script1.js:4 script1 element 

? hello world ?

? script1.js:8 script1 finishes running at 1496751598679 script2.js:2 script2 is running at 1496751598680 script2.js:4 script2 element

? hello world ?

? script2.js:8 script2 finishes running at 1496751600680

Conclusion:

No page delay and blank page when opening the page in browser, as scripts are put/run after the DOM is ready.

Scripts can correctly fetch DOM elements.

Scripts themselves are blocking each other, as the same as the first example.

However, puting all scripts at the bottom of body sometimes doesn"t fit in some specific real life cases. For example, if we aim to calculate the ATF[https://en.wikipedia.org/wiki...] of a page, we can"t simple wait for the DOM to render. We have to load and run some scripts beforehand and then fire the ATF marker when the ATF is ready, which is definitely before DOM is ready for web pages with scrolling content. Therefore, it seems reasonable to come out with the next solution.

Put scripts seperately in head and body based on requirements.

Put scripts that needs pre-running at head and others at the bottom of body. E.g.

 
     
         
    
defer!

The main disadvantage of putting scripts at the bottom of body is that the scripts will only be retrieved/loaded after the DOM is rendered. As we said, retrieving/loading the script content is NON-BLOCKING while running the script content is the BLOCKING part. We can improve the web performance if we can retrieve/load the scripts while the DOM is rendering, rather than wait for the DOM to complete rendering. This works pretty well especially when the scripts are large. This is why defer is introduced. defer loads the scripts simultaneously but only runs the scripts after the DOM is ready.



    
         test js tag async and defer attributes
        
        
    
    
        

hello world

The console output:

script1 is running at 1496760312686
script1.js:4 script1 element 

? hello world ?

? script1.js:8 script1 finishes running at 1496760313686 script2.js:2 script2 is running at 1496760313686 script2.js:4 script2 element

? hello world ?

? script2.js:8 script2 finishes running at 1496760315686

Conclusion:

The result is the same as putting scripts at the bottom of body. We can conclude from the result that the scripts are run after the DOM is ready as we can indeed fetch the DOM elements.

Even the defered scripts follow the order rule specified in html, script1 is run after script2.

async!

Web pages often contain some add-on features which are strictly independently and NOT must-to-have, such as the comment and chat functionalities on some pages. As the features are independent, they don"t have the running order restriction. In this case, we can use async



    
         test js tag async and defer attributes
    
    
        

hello world

We can observe different console outputs when we refresh the page:

The running order of script1 and script2 varies

The result of fetching DOM element is inconsistent

As async scripts don"t guarantee the running order, this is often the source of potential hidden bugs. Have a second thought before using async and mare sure these scripts are strictly independent.

Conclusion

The general rule to import script is:

 
     
        
         
     
    
        
        

hello world

Code Sample

Reference

Async JavaScript

High Performance Web Sites

Notice

If you benefit from this Repo,Please「Star 」to Support.

If you want to follow the latest news/articles for the series of reading notes, Please 「Watch」to Subscribe.

文章版權(quán)歸作者所有,未經(jīng)允許請勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉(zhuǎn)載請注明本文地址:http://specialneedsforspecialkids.com/yun/93338.html

相關(guān)文章

  • Five minutes to understand async and defer

    摘要:,, Script tag When we want to insert a script into a web page, the standard way is to use the script tag(i.e. ). The first impression that comes to peoples mind about script tag is - BLOCKING ....

    Alex 評論0 收藏0
  • promise, async, await, execution order

    摘要: async can be transformed to promise. So, if we want to understand async, we have to understand promise first. Promise Normally, promise is easy to understand, especially when using like this: p...

    neuSnail 評論0 收藏0
  • 2017-07-11 前端日報(bào)

    摘要:前端日報(bào)精選月份前端資源分享第期打包實(shí)戰(zhàn)上大漠窮秋全面解讀核心特性拖放什么是裝飾器應(yīng)該在什么時候使用裝飾器中文全棧工程師的自我修養(yǎng)濃縮筆記下做工程師不做碼農(nóng)常用布局簡潔解決方案白底黑字進(jìn)階試試酷炫的視角技術(shù)周刊期知乎專欄實(shí)踐當(dāng) 2017-07-11 前端日報(bào) 精選 7月份前端資源分享【第992期】webpack 2 打包實(shí)戰(zhàn)(上)大漠窮秋:全面解讀Angular 4.0核心特性HTML...

    _ivan 評論0 收藏0
  • DOM中的各種區(qū)別小節(jié)

    摘要:歡迎光臨小弟博客我的博客原文中的各種區(qū)別小節(jié)參考普通添加事件和事件綁定的事件監(jiān)聽與捕獲和冒泡和的區(qū)別 相信大家在DOM的實(shí)際開發(fā)與學(xué)習(xí)過程中,肯定也遇到不少需要比較的東西,這里我主要列比較以下幾點(diǎn),更多的區(qū)別和總結(jié),希望想到和遇到的朋友給我留言哦。 clientHeight/scrollHeight/offsetHeight defer vs async 事件模型-捕獲/目標(biāo)/冒泡...

    Guakin_Huang 評論0 收藏0
  • Async and Defer

    async and defer async ??There are actually two ways we can bypass the problem of the blocking script — async and defer. ??Async scripts will download the script without blocking rendering the page and...

    JackJiang 評論0 收藏0

發(fā)表評論

0條評論

最新活動
閱讀需要支付1元查看
<