码迷,mamicode.com
首页 > Web开发 > 详细

JS计时器

时间:2018-08-23 11:22:01      阅读:248      评论:0      收藏:0      [点我收藏+]

标签:time()   idt   1.0   init   time   func   imu   har   tin   

<!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计时器

标签:time()   idt   1.0   init   time   func   imu   har   tin   

原文地址:https://www.cnblogs.com/shenbo666/p/9522578.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!