1. 程式人生 > >js_外部對象——時鐘列子

js_外部對象——時鐘列子

獲取 ava htm border string etime date itl title

<!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_外部對象——時鐘列子