杰ღ᭄ꦿ࿐ 2022-02-17 21:35 采纳率: 33.3%
浏览 69
已结题

每秒产生并打印递增的自然数1、2、3...,直到100为止,点击按钮暂停/继续

能暂停,但想不通如何继续,如何点击之后续着之前的值打印下去

btn.onclick = function () {
            if (this.innerText === "暂停") {
                clearInterval(id);
                this.innerText = "继续";
            } else {
                this.innerText = "暂停";
            }
            var num = 1;
            var id = setInterval(
                function () {
                    console.log(num);
                    num++;
                    if (num === 101) {
                        clearInterval(id);
                    }
                }, 1000
            )
        }

  • 写回答

3条回答 默认 最新

  • 关注

    你题目的解答代码如下:

    <!doctype html>
    <html lang="en">
    <head>
        <meta charset="UTF-8" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
        <title> 页面名称 </title>
    </head>
    <body>
    
        <button id="btn" type="button" >开始</button>
    
        <script type="text/javascript">
            var btn = document.getElementById("btn");
            var id;
            var num = 1;
            btn.onclick = function () {
                if (this.innerText === "暂停") {
                    clearInterval(id);
                    this.innerText = "继续";
                } else {
                    this.innerText = "暂停";
                    id = setInterval(
                        function () {
                            console.log(num);
                            num++;
                            if (num === 101) {
                                clearInterval(id);
                            }
                        }, 1000
                    )
                }
            }
        </script>
    </body>
    </html>
    

    如有帮助,请点击我的回答下方的【采纳该答案】按钮帮忙采纳下,谢谢!

    img

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录
查看更多回答(2条)

报告相同问题?

问题事件

  • 系统已结题 2月26日
  • 已采纳回答 2月18日
  • 修改了问题 2月17日
  • 创建了问题 2月17日

悬赏问题

  • ¥20 CST怎么把天线放在座椅环境中并仿真
  • ¥15 任务A:大数据平台搭建(容器环境)怎么做呢?
  • ¥15 r语言神经网络自变量重要性分析
  • ¥15 基于双目测规则物体尺寸
  • ¥15 wegame打不开英雄联盟
  • ¥15 公司的电脑,win10系统自带远程协助,访问家里个人电脑,提示出现内部错误,各种常规的设置都已经尝试,感觉公司对此功能进行了限制(我们是集团公司)
  • ¥15 救!ENVI5.6深度学习初始化模型报错怎么办?
  • ¥30 eclipse开启服务后,网页无法打开
  • ¥30 雷达辐射源信号参考模型
  • ¥15 html+css+js如何实现这样子的效果?