<!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>
div{
width: 300px;
margin: 20px 0;
line-height: 30px;
background: yellowgreen;
}
</style>
</head>
<body>
<button id="start">开始</button>
<button id="stop">停止</button>
<div id="box"></div>
<script>
const startBtn = document.getElementById('start')
const stopBtn = document.getElementById('stop')
const box = document.getElementById('box')
var lock = true;
var show = {
constent:'Hello Wold',
timer:null,
start:function(){
if(!lock) return;
lock = false;
startBtn.addEventListener('click',()=>{
timer = setInterval(() => {
box.innerHTML +=show.constent;
}, 1000);
console.log(lock)
})
timer = setInterval(() => {
lock = true;
}, 1000);
},
stop:function(){
stopBtn.addEventListener('click',function(){
clearInterval(timer)
})
}
}
show.start()
show.stop()
</script>
</body>
</html>