<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
#showTime {
width: 300px;
height: 60px;
font-size: 60px;
line-height: 60px;
}
</style>
</head>
<body>
<div>
<div id="showTime">00:00:00</div>
<button id="startBn">启动</button>
<button id="restBn">复位</button>
</div>
<script>
var time, showTime, startBn, restBn, pauseDate;
var bool = false;
var pauseTime = 0;
init();
function init() {
showTime = document.getElementById("showTime");
startBn = document.getElementById("startBn");
restBn = document.getElementById("restBn");
startBn.addEventListener("click", clickHandler);
restBn.addEventListener("click", clickHandler);
setInterval(animation, 16);
}
function animation() {
if (!bool) return;
var times = new Date().getTime() - time - pauseTime;
var minutes = Math.floor(times / 60000);
var seconds = Math.floor((times - minutes * 60000) / 1000);
var ms = Math.floor((times - minutes * 60000 - seconds * 1000) / 10);
showTime.innerHTML =
(minutes < 10 ? "0" + minutes : minutes) + ":"
+ (seconds < 10 ? "0" + seconds : seconds) + ":"
+ (ms < 10 ? "0" + ms : ms);
}
function clickHandler(e) {
e = e || window.event;
if (this === startBn) {
bool = !bool;
if (bool) {
this.innerHTML = "暂停";
pauseTime += (!pauseDate ? 0 : new Date().getTime() - pauseDate);
if (time) return;
time = new Date().getTime();
return;
}
this.innerHTML = "启动";
pauseDate = new Date().getTime();
return;
}
startBn.innerHTML = "启动";
pauseTime = 0;
pauseDate = null;
bool = false;
time = 0;
showTime.innerHTML = "00:00:00";
}
</script>
</body>
</html>