HLJ 发布于
2022-10-01 11:53:21

JavaScript new Image()计算图片加载完成的时间

<!DOCTYPE HTML>  
<html>  
<head>
<img id="img">
<div id="time"></div>
</head> 
<body>  
<script type="text/javascript">
  let num = 0
    const imageObj = new Image();
  const timer = setInterval(() => {
      document.getElementById('time').innerHTML = num++
      imageObj.onload = () => {
        document.getElementById('img').src = imageObj.src
        console.log(num)
        clearInterval(timer);
      }
  },1000)

  imageObj.src = 'http://good1230.com/templates/test_img/1.jpg';
</script>
</body>  
</html>  
当前文章内容为原创转载请注明出处:http://www.good1230.com/detail/2022-10-01/604.html
最后生成于 2022-11-21 22:22:30