<!doctype html>
<html>
<head>
<meta charset="utf-8">
<link rel="stylesheet" href="css/css布局实践.css">
<script type="text/javascript">
function clock(){
var time = new Date();
var hour = time.getHours();
var minute = time.getMinutes();
var second = time.getSeconds();
var noon = "AM";
if(hour>12){
noon = "PM";
}
if(hour<10){
hour = "0"+hour;
}
if(minute<10){
minute = "0"+minute;
}
if(second<10){
second = "0"+second;
}
var clock_text = hour+":"+minute+":"+second+" "+noon;
var dis_text = document.createTextNode(clock_text);
var dis = document.getElementById("display");
dis.appendChild(dis_text);
setTimeout(clock(),10000);
}
</script>
<title>无标题文档</title>
</head>
<body onLoad="clock()">
<div id="display"></div>
</body>
</html>这个错误是怎么造成的,按说这个函数应该每间隔一秒钟为id为display的div元素添加一个文本节点啊。。。怎么一下子会出来那么多呢
2 回答
已采纳
_亓
TA贡献33条经验 获得超29个赞
每隔一秒应该是1000毫秒,你写的是10000。然后就是你的格式写错了应该是setTimeout(“clock()”,10000);函数名外面要加双引号或者单引号的
merry_离
TA贡献2条经验 获得超0个赞
function clock(){ setInterval(function() { var time = new Date(); var hour = time.getHours(); var minute = time.getMinutes(); var second = time.getSeconds(); var noon = "AM"; if(hour>12){ noon = "PM"; } if(hour<10){ hour = "0"+hour; } if(minute<10){ minute = "0"+minute; } if(second<10){ second = "0"+second; } var clock_text = hour+":"+minute+":"+second+" "+noon; var dis_text = document.createTextNode(clock_text); var dis = document.getElementById("display"); dis.appendChild(dis_text); }, 10000);
对你的代码做了修改,如果想隔一段时间的触发一次可以用 setInterval()这个方法,不需要用setTimeout()
setInterval()的作用是每隔多少时间触发一次,setTimeout()是只触发一次。
添加回答
举报
0/150
提交
取消