自动更新图片 [英] Auto update Image

查看:90
本文介绍了自动更新图片的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一张图片,我想使用javascript,jquery或ajax自动更新它.

I have an image and I want to automatically update it using either javascript, jquery or ajax.

直到现在我有以下代码.

Until now I have the following code.

<html>

<head>
<script language="JavaScript"><!--
  function refreshIt() {
    if (!document.images) return;
      document.images['myCam'].src = 'swt.png';
      setTimeout('refreshIt()',50); // refresh every 50ms
 }
  //--></script>
</head>

<body onLoad=" setTimeout('refreshIt()',50)">

<img src="swt.png" name="myCam">

</body>

</html>

我认为它不起作用,因为浏览器正在缓存图像,并且无法正确更新图像.

I think it is not working because the browser is caching the image and it is not updating the image correctly.

有人可以给我提供一个可行的例子吗?

Can someone provide me a working example?

推荐答案

这可以解决问题,但就性能而言,这是一场噩梦.

This will do the trick, but it's a nightmare as far as performance.

<html>
    <head>
        <script language="JavaScript">
            function refreshIt(element) {
                setTimeout(function() {
                    element.src = element.src.split('?')[0] + '?' + new Date().getTime();
                    refreshIt(element);
                }, 50); // refresh every 50ms
            }
        </script>
    </head>
    <body>
        <img src="swt.png" name="myCam" onload="refreshIt(this)">
    </body>
</html>

这篇关于自动更新图片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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