老师,请检查下代码对不对?

来源:3-13 编程练习

leepulse

2021-02-24 13:15:58

<!DOCTYPE html>

<html>

  <head>

    <meta charset="utf-8" />

    <title>Document</title>

    <style>

      div {

        width300px;

        margin20px 0;

        line-height30px;

        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");

      let timer;

      var show = {

        content: "Hello World ",

        timer: null,

        start: ()=>

          // 在此补充代码

          startBtn.addEventListener("click",()=>timer = setInterval(()=>box.innerHTML += `${show.content}`1000),false),

        stop: () =>

        // 在此补充代码

          stopBtn.addEventListener("click", () => clearInterval(timer), false)

      };

      // 在此补充代码

      show.start();

      show.stop();

    </script>

  </body>

</html>




写回答

2回答

好帮手慕张

2021-02-24

同学你好,“timer:null”就是定义了一个为空的timer变量,这个timer变量和同学在外面声明的变量的作用是一样的,因为同学在外面使用let又声明了一个timer变量,所以这里使用的都是外面声明的那个变量。

老师提供一种使用预留代码实现的思路,同学可参考理解一下,如下图:

http://img.mukewang.com/climg/60360b3809000c3305850668.jpg

祝学习愉快!

0

好帮手慕张

2021-02-24

同学你好,代码效果正确,继续加油。祝学习愉快!

0
heepulse
hp dir="ltr">我想问下,show对象中的timer:null这个有什么用,我想不到这个是怎么用到清除定时器的,在这练习题中,点开始按钮时我也忘了加上先清除定时器再设置定时器

h021-02-24
共1条回复

0 学习 · 15276 问题

查看课程