一直在研究script标签中async
和defer
的使用。鉴于以下...
async - Allows the rest of the html to parse, while the JS script is being loaded. Once the script is completely loaded then the html parser is paused to execute the script. However, note that the order of loading can't be controlled.
defer - Also allows the rest of the html to parse, however the loaded js is only executed once the html parsing is complete.
So, if you can afford to show your html without loading your js script first... use
async
. If more than one script is involved and the order they are loaded matters then usedefer
. On the other hand, if your scripts MUST be loaded first (and can't be rewritten to allow the html to parse first) then just load the script without usingasync
ordefer
in the header.
这如何适应像 AngularJS 这样的现代框架……因为延迟加载会导致错误。任何指针将不胜感激。
最佳答案
考虑到 Angular 应用程序完全依赖于 Angular JS 文件和依赖项,延迟或异步它们没有多大意义,除非你有后备(比如加载状态,也许?)。然而,话虽如此,为所有脚本添加 defer 可能会很好,但在性能方面作用不大。添加异步可能会破坏事情,因为某些依赖项需要在其他依赖项之前加载。
关于javascript - 实际使用脚本异步或延迟等框架,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34915608/