JS計時器
阿新 • • 發佈:2018-08-23
time() -s += btn lan minimum 初始化 cti body
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<div>當前時間為:<span id="timeNow">00:00:00</span></div>
<button id="timeBegin">計時開始</button>
<button id="timeEnd">計時結束</button>
<button id="timeClear">計時清除</button>
<script>
//定義初始值 計時器
var timer = "";
//開始計時
var beginBtn = document.getElementById("timeBegin");
var endBtn = document.getElementById("timeEnd");
var clearBtn = document.getElementById("timeClear");
var min,second,millisecond;//分 秒 毫秒
min= second=millisecond=0; //初始化
function time() {
millisecond+=50;
if (millisecond >= 1000) {
millisecond=0
second += 1;
}
if (second >= 60) {
second=0
min += 1;
}
counts = min + ‘:‘ + second + ‘:‘ + millisecond;
document.getElementById("timeNow").innerText = counts;
}
//開始計時
function BeginTime() {
beginBtn.onclick = function () {
timer = setInterval(time, 50);
}
}
//結束計時
function EndTime() {
endBtn.onclick = function () {
clearInterval(timer);
}
}
//計時清除
function ClearTime() {
clearBtn.onclick = function () {
document.getElementById("timeNow").innerHTML = "00:00:00";
}
}
BeginTime();
EndTime();
ClearTime()
</script>
</body>
</html>
JS計時器