<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>计时器</title>
</head>
<body>
<button onclick="star()">开始</button>
<button onclick="end()">结束</button>
<hr />
<p id="timestar"></p>
<script>
var id1;
function star(){
alert("计时开始");
//间隔时间为1s;
id1=setInterval("fun1()",1000);
}
function fun1(){
var date=new Date();
var year=date.getFullYear();
var month=((date.getMonth()+1) > 9 ? (date.getMonth()+1) : "0" +date.getMonth());
var day=date.getDate();
var hour=date.getHours();
var min=date.getMinutes();
var sec=((date.getSeconds()+1) >9 ?(date.getSeconds()+1):"0"+date.getSeconds());
var time1=year+"年"+month+"月"+day+"日"+hour+":"+min+":"+sec;
document.getElementById("timestar").innerHTML=time1;
}
function end(){
clearInterval(id1);
alert("计时结束");
}
</script>
</body>
</html>
JavaScript计时器
最新推荐文章于 2024-06-05 15:33:33 发布