3-13编程练习为什么实现不了啊
来源:3-13 编程练习
球球不一般
2021-12-21 19:56:07
const startBtn = document.getElementById('start')
const stopBtn = document.getElementById('stop')
const box = document.getElementById('box')
var show = {
content: "Hello World",
timer: null,
start: function () {
setInterval(function () {
box.innerHTML = this.content
}, 1000)
},
stop: function () {
box.onclick = function () {
clearInterval(this.timer)
}
},
}
// 在此补充代码
搜索
复制
2回答
好帮手慕久久
2021-12-22
同学你好,“烟雨平生_Elisha”这位同学的回复基本正确,老师把他的方案,给同学添加点注释,同学认真看看:

祝学习愉快!
Elisha666
2021-12-22
1、没有将定时器
赋值给timer
2、定时器里要使用箭头函数
3、box.innerHTML = this.content 错了,改成 box.innerHTML = `${this.content} `
4、stop里的onclick要绑定给stopBtn
5、没有调用start和stop方法
可以参考我的
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Document</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 show = {
content: "Hello World",
timer: null,
start: function () {
// 在此补充代码
startBtn.onclick = () => {
this.timer = setInterval(() => {
box.innerHTML += `${this.content} `;
}, 1000);
};
},
stop: function () {
// 在此补充代码
stopBtn.onclick = () => {
clearInterval(this.timer);
};
},
}
// 在此补充代码
show.start();
show.stop();
</script>
</body>
</html>相似问题