Javascript图像刷新

时间:2012-09-05 19:36:58

标签: javascript refresh

我正在使用以下代码尝试在我的网站上刷新两个网络摄像头图像。

var t_webcam = 60 // interval in seconds 
      image_webcam = "cam_1.jpg" //name of the image 
      function Start_webcam() { 
      tmp_webcam = new Date(); 
      tmp_webcam = "?"+tmp_webcam.getTime() 
      document.images["frontcam"].src = image_webcam+tmp_webcam 
      setTimeout("Start_webcam()", t_webcam*1000) 
      } 
      Start_webcam();

在网站上,我使用以下代码行调用该函数

然而,当我将其加载到网站上时,我收到以下错误并且它不起作用,

  

未捕获的TypeError:无法设置未定义的属性'src'

如果有人在Javascript上有更多的知识,并且能够提供一些帮助,我会很高兴。

由于 理查德

4 个答案:

答案 0 :(得分:2)

document.images是一个数组,所以它只接受数字索引。您试图像字典一样访问它,将frontcam作为密钥传递。

如果您的代码如下所示,则可以使用document.getElementById

<img src="sth.jpg" id="frontcam"></img>

答案 1 :(得分:0)

该行:

document.images["frontcam"].src = image_webcam+tmp_webcam

无效,它可能应该是更像这样的东西:

 document.getElementById("frontcam").src = image_webcam+tmp_webcam;

答案 2 :(得分:0)

我认为frontcam是一个ID?

尝试替换

document.images["frontcam"].src

document.getElementById('frontcam').src

答案 3 :(得分:0)

如果id="frontcam"标记尚未添加<img>,请添加var t_webcam = 60; // interval in seconds var image_webcam = "cam_1.jpg"; //name of the image function Start_webcam() { var tmp_webcam = new Date(); tmp_webcam = "?"+tmp_webcam.getTime(); document.getElementById("frontcam").src = image_webcam+tmp_webcam; setTimeout(Start_webcam, t_webcam*1000); } Start_webcam(); ,然后尝试使用此代码

{{1}}