<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>时钟、定时器</title>
<style type="text/css">
#clock{ height:20px; background:#000; color:#FFF}
</style>
</head>
<body>
<div id="clock"></div>
</body>
</html>
<script>
timer=null;
function myClock(){
var d=new Date;
var h=d.getHours();
var m=d.getMonth();
var s=d.getSeconds();
var ms=d.getMilliseconds();
var clock=document.getElementById('clock');
clock.innerHTML=h+":"+m+":"+s+":"+ms;
//timer=window.setTimeout(myClock,1000);
//timer=window.setTimeout(arguments.callee,1000);//执行一次
//arguments只能用在里面
console.log(timer);
}
//myClock();
clock.οnclick=function(){
if(timer){
clearTimeout(timer);
//clearInterval(timer);//通用
timer=null;
console.log(timer);
}else{
timer=window.setInterval(myClock,1000);
}
}
//window.setInterval(myClock,1000);//不要写到里面,2的n次方了
</script>