<html>
<head>
<title></title>
<script>
function loadTimer(){
var now=new Date;
var year=now.getYear();
var month=now.getMonth()+1;
var day=now.getDate();
if(month<10) month="0"+month;
if(day<10) day="0"+day;
// alert(year+"-"+month+"-"+day);
var hour=now.getHours();
var minute=now.getMinutes();
var second=now.getSeconds();
if(hour<10) hour="0"+hour;
if(minute<10) minute="0"+minute;
if(second<10) second="0"+second;
// alert(year+"-"+month+"-"+day+" "+hour+":"+minute+":"+second);
clock.innerHTML="当前系统时间为:"+year+"-"+month+"-"+day+" "+hour+":"+minute+":"+second;
setTimeout("loadTimer()",1000);
}
</script>
</head>
<body onload="loadTimer()">
<div style="font-size:16;color:#f355ff" id="clock"></div>
</body>
</html>
<head>
<title></title>
<script>
function loadTimer(){
var now=new Date;
var year=now.getYear();
var month=now.getMonth()+1;
var day=now.getDate();
if(month<10) month="0"+month;
if(day<10) day="0"+day;
// alert(year+"-"+month+"-"+day);
var hour=now.getHours();
var minute=now.getMinutes();
var second=now.getSeconds();
if(hour<10) hour="0"+hour;
if(minute<10) minute="0"+minute;
if(second<10) second="0"+second;
// alert(year+"-"+month+"-"+day+" "+hour+":"+minute+":"+second);
clock.innerHTML="当前系统时间为:"+year+"-"+month+"-"+day+" "+hour+":"+minute+":"+second;
setTimeout("loadTimer()",1000);
}
</script>
</head>
<body onload="loadTimer()">
<div style="font-size:16;color:#f355ff" id="clock"></div>
</body>
</html>
本文介绍了一种使用JavaScript中的定时器来实时更新网页上显示的时间的方法。通过获取当前日期和时间,并将其格式化为易读的形式,再利用`setTimeout`函数每秒更新一次时间显示,实现了动态的时间显示效果。

被折叠的 条评论
为什么被折叠?



