<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" charset="UTF-8">
<title></title>
<style type="text/css">
#show{width: 500px;height: 500px;}
</style>
<script language="javascript" type="text/javascript">
window.onload = function(){
showtime();
}
function checkTime(i)
{
if (i<10)
{i="0" + i}
return i
}
function showtime()
{
var now=new Date();
var year=now.getFullYear();
var month=now.getMonth()+1;
var day=now.getDate();
var h=now.getHours();
var m=now.getMinutes();
var s=now.getSeconds();
m=checkTime(m);
s=checkTime(s);
var week=new Array(7)
week[0]="星期日"
week[1]="星期一"
week[2]="星期二"
week[3]="星期三"
week[4]="星期四"
week[5]="星期五"
week[6]="星期六"
document.getElementById("show").innerHTML=""+year+"年"+month+"月"+day+"日"+week[now.getDay()]+h+":"+m+":"+s ;
t=setInterval("showtime()",500)
}
</script>
</head>
<body>
<div id="show">dfsfd</div>
</body>
</html>
1 回答
已采纳
Caballarii
TA贡献1123条经验 获得超629个赞
因为你每次执行showtime都生成一个新的定时器,不需要多久就定时器太多内存溢出了。
所以把setInterval写到showtime外面或者把setInterval改成setTimeout
添加回答
举报
0/150
提交
取消