<!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>Document</title>
</head>
<body>
<input type="text">
<button id="start">开始计数</button>
<button id="stop">停止计数</button>
<script>
// 获取开始计数按钮
var start = document.getElementById("start");
var num = 0;
var timer ;
start.onclick = function(){
timer = setInterval(function(){
num++;
console.log(num);
document.querySelector("input").value = num;
}, 1000);
}
document.getElementById("stop").onclick = function(){
// 停止定时器
clearInterval(timer);
}
</script>
</body>
</html>