2016-04-03 103 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

好吧,我会尝试提供工作现场示例。 –