JS计时器
Posted 小猴子会上树
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了JS计时器相关的知识,希望对你有一定的参考价值。
<!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计时器的主要内容,如果未能解决你的问题,请参考以下文章