用于知道图像何时加载的Javascript回调 [英] Javascript callback for knowing when an image is loaded

查看:120
本文介绍了用于知道图像何时加载的Javascript回调的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道图片加载完成的时间。有回调的方法吗?

I want to know when an image has finished loading. Is there a way to do it with a callback?

如果没有,是否有办法呢?

If not, is there a way to do it at all?

推荐答案

Image.onload()通常有效。

要使用它,您需要在设置src属性之前绑定事件处理程序。

To use it, you'll need to be sure to bind the event handler before you set the src attribute.

相关链接:

示例用法:

<html>
<head>
<title>Image onload()</title>
</head>
<body>

<img src="#" alt="This image is going to load" id="sologo"/>

<script type="text/javascript">
window.onload = function () {

    var logo = document.getElementById('sologo');

    logo.onload = function () {
        alert ("The image has loaded!");        
    };

    setTimeout(function(){
        logo.src = 'http://stackoverflow.com/Content/Img/stackoverflow-logo-250.png';         
    }, 5000);
};
</script>
</body>
</html>

这篇关于用于知道图像何时加载的Javascript回调的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆