利用javaScript实现时间实时更新
话不多说,贴代码,有什么不足的地方请告诉我,谢谢!或者有什么不懂得地方可以留言
function getTime(){
str = "当前时间为:"
var p = document.getElementById("timeP");
time = new Date();
year = time.getFullYear();
month = time.getMonth() + 1;
day = time.getDate();
hour = time.getHours();
minutes = time.getMinutes();
seconds = time.getSeconds();
str = str + year +"-"+ month +"-"+ day + " " +hour+":"+minutes+":"+seconds;
p.innerText = str;
setTimeout(getTime,1000);
}
window.onload = function(){
getTime();
}<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> <script type="text/javaScript" src="js/index.js"></script> <script type="text/javaScript" src="js/time.js"></script> </head> <body> <p id = "timeP"></p> </body> </html>
声明:该文观点仅代表作者本人,牛骨文系教育信息发布平台,牛骨文仅提供信息存储空间服务。
