2010-10-11 45 views
1

我試圖創建一個頁面,用於顯示和刷新網絡攝像頭的圖像(camimg.jpg),但如果圖像最近未更新,則會顯示靜態圖像(notlive.jpg)。我已經找到了AJAXCam腳本,該腳本用於在設定的間隔重新加載「活」形象的目的(15秒),但與沒有使用Javascript的經驗,我無法弄清楚如何確定當圖像上次更新以決定是否顯示camimg.jpg或notlive.jpg。確定在Javascript中的圖像的創建日期?

我與編程有限的經驗我想它應該是沿着線的東西:

pageLoaded = time page loaded in browser 
imageUpdated = time the image was uploaded to server 

if imageUpdated is not within 20 seconds of pageLoaded: 
    display notlive.jpg 
else: 
    run AJAXCam 

的AJAXCam代碼(最初由<body onLoad="holdUp()">的稱呼)如下:

<script type="text/javascript"> 
<!-- 
// 
//AJAXCam v0.8b (c) 2005 Douglas Turecek http://www.ajaxcam.com 
// 
function holdUp() 
{ 
// 
// This function is first called either by an onLoad event in the <body> tag 
// or some other event, like onClick. 
// 
// Set the value of refreshFreq to how often (in seconds) 
// you want the picture to refresh 
// 
refreshFreq=15; 
// 
//after the refresh time elapses, go and update the picture 
// 
setTimeout("freshPic()", refreshFreq*1000); 
} 

function freshPic() 
{ 
// 
// Get the source value for the picture 
// e.g. http://www.mysite.com/doug.jpg and 
// 
var currentPath=document.campic.src; 
// 
// Declare a new array to put the trimmed part of the source 
// value in 
// 
var trimmedPath=new Array(); 
// 
// Take everything before a question mark in the source value 
// and put it in the array we created e.g. doug.jpg?0.32234 becomes 
// doug.jpg (with the 0.32234 going into the second array spot 
// 
trimmedPath=currentPath.split("?"); 
// 
// Take the source value and tack a qustion mark followed by a random number 
// This makes the browser treat it as a new image and not use the cached copy 
// 
document.campic.src = trimmedPath[0] + "?" + Math.random(); 
// 
// Go back and wait again. 
holdUp(); 
} 

// --> 
</script> 

是什麼我試圖完成甚至可能?如果是這樣,我將如何去實施它?先謝謝您的幫助!

+0

對於那些誰可能會發現這個問題,並面臨着類似的問題,我發現cavemonkey50的[網絡攝像頭更新腳本](http://cavemonkey50.com/code/webcam_update_script/)。將他的PHP和AJAXCam的腳本結合起來,實現了我原本打算的結果。 – bobby 2010-10-11 20:05:59

回答

1

有沒有辦法做到這一點在JavaScript。

你唯一的選擇是,如果你有訪問服務器端語言如PHP,您可以發送一個簡單的HEAD請求,並檢查最後的圖像Modified頭然後報告回通過Ajax的瀏覽器。

+0

感謝您的輸入! – bobby 2010-10-11 19:59:41

相關問題