javascript - Use Js or Jq to determine whether a resource is loaded
天蓬老师
天蓬老师 2017-05-18 10:57:54
0
4
665

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?

天蓬老师
天蓬老师

欢迎选择我的课程,让我们一起见证您的进步~~

reply all(4)
洪涛

The browser loads these resources or sends them asynchronouslyhttp请求的话,一般都会提供钩子函数.

For example, you get the jscode 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 scriptWhen 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

PHPzhong

onload event or readystatechange event, but not all objects support it. In addition, promises should also work

PHPzhong

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.
阿神

You can consider ajax or fetch API

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template