javascript - 用Js或者Jq判断一个资源是否加载完毕
天蓬老师
天蓬老师 2017-05-18 10:57:54
[JavaScript讨论组]

比如一个图片,等它加载完毕,也就是请求到之后,再去执行另一个操作。

那其它资源类型呢,比如swf是否加载完,是否是同样的方法?

或者一个http请求又是如何监控呢?

天蓬老师
天蓬老师

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

全部回复(4)
PHP中文网

浏览器去加载这些资源或者是异步去发http请求的话,一般都会提供钩子函数

例如,你异步去获取js代码:

    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)

你创建的script对象上提供了加载的钩子函数:onload/onreadystatechange(浏览器的兼容性处理),当你把script标签插入到DOM中时,浏览器另开一个线程去异步加载你需要的js资源,在加载的不同阶段:this.readyState对应不同的值,这个时候根据需要设置你的回调函数。这样就完成了对于一段js代码异步获取的监控。

PHPz

onload事件或者readystatechange事件,但不是所有对象都支持。另外promise应该也能吧

PHPz

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

可以考虑ajax或者fetch API

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号