【问题标题】:Javascript Image RefreshJavascript 图像刷新
【发布时间】:2012-09-05 19:36:57
【问题描述】:

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

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();

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

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

未捕获的类型错误:无法设置未定义的属性“src”

如果有人对 Javascript 有更多的了解并能够提供一些帮助,我将非常感激。

谢谢 理查德

【问题讨论】:

    标签: javascript refresh


    【解决方案1】:

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

    如果您的标签如下所示,您可以使用document.getElementById

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

    【讨论】:

      【解决方案2】:

      行:

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

      无效,应该是这样的:

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

      【讨论】:

        【解决方案3】:

        id="frontcam" 添加到您的&lt;img&gt; 标记(如果它还没有),然后尝试此代码

        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();
        

        【讨论】:

          【解决方案4】:

          我假设 frontcam 是一个 ID?

          尝试替换

          document.images["frontcam"].src
          

          document.getElementById('frontcam').src
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2013-05-15
            • 2013-08-19
            • 2016-01-02
            • 1970-01-01
            • 2023-03-18
            • 2015-07-12
            • 2012-01-01
            • 2013-05-01
            相关资源
            最近更新 更多