setInterval 计时器

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title> New Document </title>
<meta name="Generator" content="EditPlus">
<meta name="Author" content="">
<meta name="Keywords" content="">
<meta name="Description" content="">
</head>

<body>
<p id="pid"></p>
<button id="btn" onclick="stopTime()">停止</button>

<script>
var mytime = setInterval(function(){
getTime();
},1000);
function getTime(){
var d=new Date(); //创建date日期对象
var t=d.toLocaleTimeString(); //将日期对象转化成字符串
document.getElementById("pid").innerHTML=t; //在文本p中打印出来
}
function stopTime(){
clearInterval(mytime);
}
</script>
</body>
</html>

 

posted @ 2016-04-04 14:21  夜未央leo  阅读(104)  评论(0编辑  收藏  举报