For example, for a picture, wait until it is loaded, that is, after the request is received, and then perform another operation.
What about other resource types, such as whether the swf is loaded? Is it the same method?
Or how to monitor an http request?
PHP中文网2017-05-18 10:59:54
The browser loads these resources or sends them asynchronouslyhttp
请求的话,一般都会提供钩子函数
.
For example, you get the js
code asynchronously:
const script = document.createElement('script')
script.src = 'xxxx'
script.onload = script.onreadystatechange = function () {
if (!this.readyState || /^(loaded|complete)$/.test(this.readyState)) {
resolve(window[namespace][name])
sc.onload = sc.onreadystatechange = null
}
}
document.body.append(script)
The script
object you created provides a loading hook function: onload/onreadystatechange
(browser compatibility processing), when you put script
When the tag is inserted into DOM
, the browser opens another thread to asynchronously load the resources you need. At different stages of loading: this.readyState
corresponds to different values. , set your callback function as needed at this time. This completes the monitoring of asynchronous acquisition of a piece of code. script
对象上提供了加载的钩子函数:onload/onreadystatechange
(浏览器的兼容性处理),当你把script
标签插入到DOM
中时,浏览器另开一个线程去异步加载你需要的js
资源,在加载的不同阶段:this.readyState
对应不同的值,这个时候根据需要设置你的回调函数。这样就完成了对于一段js
PHPz2017-05-18 10:59:54
onload event or readystatechange event, but not all objects support it. In addition, promises should also work
PHPz2017-05-18 10:59:54
http://stackoverflow.com/ques...
Use SWFObject to embed the SWF, then use the callback function to poll the SWF's PercentLoaded value.
If the value is 0, the SWF has not loaded yet. When it hits 100, the SWF is fully loaded.
Here's a tutorial for polling PercentLoaded, complete with code examples.