爱逗猫的鱼
2016-09-16 21:47
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>定时器</title>
<script type="text/javascript">
function clock(){
var attime;
var time=new Date();
var h=time.getHours();
var m=time.getMinutes();
var s=time.getSeconds();
m=checkTime(m);
s=checkTime(s);
attime=h+":"+m+":"+s;
document.getElementById("clock").value = attime;
setInterval("clock()",100);
}
function checkTime(i){
if(i<10){
i="0"+i;
}
return i;
}
</script>
</head>
<body>
<form>
<input type="text" id="clock" size="50" />
<input type="button" onclick="clock()" value="显示时间" />
</form>
</body>
</html>
setInterval("clock()",100);这一句不能放在定义的函数体内,放到函数体外。即,跟它下一行的}调换下位置看看
JavaScript进阶篇
468061 学习 · 21891 问题
相似问题