<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>秒表</title>
<style>
* {
padding: 0;
margin: 0;
}
.head {
width: 200px;
height: 100px;
margin: 50px auto;
}
.box {
width: 200px;
height: 50px;
background-color: palevioletred;
border-radius: 10px;
}
#sj {
margin-left: 70px;
line-height: 50px;
}
button {
width: 40px;
height: 20px;
font-size: 12px;
}
</style>
</head>
<body>
<div class="head">
<div class="box">
<span id="sj">11:20:00</span>
</div>
<button id="pause">暂停</button>
<button id="action">开始</button>
</div>
</body>
<script>
function getDate() {
dobj = new Date();
hour = dobj.getHours();
minute = dobj.getMinutes();
second = dobj.getSeconds();
str = hour + ":" + minute + ":" + second;
mb = document.getElementById("sj");
mb.innerHTML = str;
}
//
getDate();
start();
function start() {
sobj = setInterval(getDate, 1000);
}
function stop() {
clearInterval(sobj);
}
b = document.getElementById("pause");
b.onclick = function () {
stop();
}
action = document.getElementById("action");
action.onclick = function () {
start();
}
</script>
</html>