時間定時器
<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta http-equiv="X-UA-Compatible" content="ie=edge" />
<title>Document</title>
<!--點選開始,就動態顯示時間,1秒重新整理一次,點選結束時間就停止-->
<script>
function startTime(){
var dateTime = new Date();
var hour = dateTime.getHours();
var min = dateTime.getMinutes();
var sec = dateTime.getSeconds();
if(hour<10){
hour = "0"+hour;
}
if(min<10){
min = "0"+min;
}
if(sec<10){
sec = "0"+sec;
}
document.getElementById("time").value = hour+":"+min+":"+sec;
sid = setTimeout('startTime()',1000);
}
function endTime(){
clearTimeout(sid);
}
</script>
</head>
<body>
<input type="button" value="開始" onclick="startTime()"/>
<input type="text" id="time" size="5"/>
<input type="button" value="結束" onclick="endTime()"/>
</body>
</html>