<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>
<input type="text" id="ip">
<button id="btn1">开始</button>
<button id="btn2">结束</button>
<script>
var t;
function showTime() {
var ip = document.getElementById(‘ip‘);
var time = new Date();
ip.value = time.toLocaleString();
}
showTime();
var btn1 = document.getElementById(‘btn1‘);
btn1.onclick = function (e) {
if (!t){
t = setInterval(showTime,1000)
};
};
var btn2 = document.getElementById(‘btn2‘);
btn2.onclick = function (e) {
clearInterval(t);
t = undefined;
};
</script>
</body>
</html>
DOM之定时器计时效果