[removed] Is there any way to detect when all async scripts have loaded?

后端 未结 1 1116
终归单人心
终归单人心 2021-01-12 04:55

If you use plain script tags on an HTML page, rendering is blocked until the script has been downloaded and parsed. To avoid that, for faster page display, you can add the \

1条回答
  •  说谎
    说谎 (楼主)
    2021-01-12 05:04

    The best you could hope for would be to know if there are any outstanding async calls (XMLHttpRequest, setTimeout, setInterval, SetImmediate, process.nextTick, Promise), and wait for there to not be one. However, that is an implementation detail that is lost to the underlying native code--javascript only has its own event loop, and async calls are passed off to the native code, if I understand it correctly. On top of that, you don't have access to the event loop. You can only insert, you can't read or control flow (unless you're in io.js and feeling frisky).

    The way to simulate one would be to track your script calls yourself, and call after all script are complete. (i.e., track every time you insert a relevant script into the event loop.)

    But yeah, the DOM doesn't provide a NoAsyncPending global or something, which is what you'd really require.

    0 讨论(0)
提交回复
热议问题