<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>时钟</title>
<style type="text/css">
#clock{
??border: 1px solid red;
??width: 200px;
??text-align: center;
??font-size: 20px;
??height: 30PX;
??line-height: 30px;
}
</style>
<script type="text/javascript">
??var id;
??//开始
??function start() {
//若id非空则定时器已启动过,那么就不要再启动了
if(id){
return;
}
id=setInterval(function(){
//获取当前客户端的时间
var d=new Date();
//获取本地格式时间
var now=d.toLocaleTimeString();
//将时间写入clock
var p=document.getElementById("clock");
p.innerHTML=now;
}, 1000);
}
??//停止
??function stop() {
??//id非空时,定时器处于启动状态,此时启动它才有效;
if(id){
clearInterval(id);
//为了再次可以启动,将id清空
id=null;
}
}
</script>
</head>
<body>
?<p>
???<input type="button" value="开始"
???onclick="start();">
???<input type="button" value="停止"
???onclick="stop();">
?</p>
?<p id="clock"></p>
</body>
</html>
js_外部对象——时钟列子
原文地址:http://www.cnblogs.com/Bighua123/p/7634281.html