2012-09-05 102 views
1

我正在使用以下代碼嘗試刷新我網站上的兩個網絡攝像頭圖像。Javascript圖片刷新

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

在我使用下面的代碼行

但是調用函數時我加載這個到網站,我碰到下面的錯誤,並忽略了最低工作現場,

Uncaught TypeError: Cannot set property 'src' of undefined

如果有人有更多關於Javascript的知識,並且能夠提供一些幫助,我會非常感激。

由於 理查德

回答

2

document.images是一個數組,所以它只能接受數值索引。您試圖以字典形式訪問它,並將frontcam作爲關鍵字。

您可以使用document.getElementById代替,如果你的代碼看起來是這樣的:

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

行:

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

是無效的,這也許應該是更多的東西是這樣的:

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

我假設frontcam是一個ID?

嘗試更換

document.images["frontcam"].src 

document.getElementById('frontcam').src 
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();