2016-04-03 66 views
1

我一直在試圖獲得一個函數來重載每一秒。Javascript獲取文件內容循環

我可以用php獲取文件內容(和元頁面重新加載),但我也使用JS來顯示時間和它不是即時加載,所以它有一個閃爍的效果。

我試過這個與JS,但它什麼也沒有顯示。

<!DOCTYPE html> 
<html> 
<head> 
<meta charset="UTF-8"> 
<title>Vivarium Enviroment Control Centre</title> 
<link rel="stylesheet" href="style.css"> 
<script src="https://code.jquery.com/jquery-1.12.0.min.js"></script> 
<script type="text/javascript"> 
    function updateTime() { 
     var currentTime = new Date(); 
     var hours = currentTime.getHours(); 
     var minutes = currentTime.getMinutes(); 
     var seconds = currentTime.getSeconds(); 
     if (minutes < 10){ 
      minutes = "0" + minutes; 
     } 
     if (seconds < 10){ 
      seconds = "0" + seconds; 
     } 
     var v = hours + ":" + minutes + ":" + seconds + " "; 
     if(hours > 11){ 
      v+="PM"; 
     } else { 
      v+="AM" 
     } 
     setTimeout("updateTime()",1000); 
     document.getElementById('time').innerHTML=v; 
    } 
updateTime(); 

$("document").ready(function(){ 
    setInterval(function(){ 
     $("#wifi").load('wifiout.php'); 
    },1000); 
}); 


function changeStatus() { 
    var image = document.getElementById('lightStatus'); 
    if (image.src.match("lightoff")) { 
     image.src = "lighton.png"; 
    } else { 
     image.src = "lightoff.png"; 
    } 
} 
</script> 
</head> 
<body> 
<div id="topbar"> 
    <span id="time"></span> 
    <div id="wifi"></div> 
    <img id="lightStatus" class="right" onclick="changeStatus()" src="lightoff.png" width="32" height="32"> 
</div> 
</body> 
</html> 
+0

你正在使用jQuery語法,但我沒有看到它包括在內? –

+0

這是我以前從未做過的事情,所以很有可能。 –

回答

1

Working fiddle

你錯過了jQuery插件的聲明,您的腳本標籤之前添加以下行:

<script src="https://code.jquery.com/jquery-1.12.0.min.js"></script> 

注:通話功能齊全內部updateTime()功能或您將收到以下錯誤:

TypeError: Cannot set property 'innerHTML' of null

因爲當頁面尚未完全加載時,您正嘗試使用span time

希望這會有所幫助。

+0

謝謝,我添加了它,但它仍然無法正常工作。 –

+0

Q已更新以反映所做的更改。 'wifiout.php'文件只包含一個圖片標籤,並且不會顯示在頁面上。 –

+0

好吧,我會嘗試提供工作現場示例。 –