定时器-延时器setTimeout./定时器bug解决方法之将定时器存在元素自己身上(元素是一个对象,对象是引用传值:安全)

01-定时器-延时器-广告案例

    <style>
        .box {
            width: 600px;
            height: 400px;
            background-color: green;
            text-align: center;
            line-height: 400px;
            /* margin: 100px auto; */

            position: fixed;
            left: 30%;

            display: none;
        }
    </style>
</head>

<body>
    <div class="box">
        这是一个广告
        <span>5</span>
    </div>
    <script>
        // 用户打开页面3秒后,弹出一个广告

        // 延时器
        let res = setTimeout(function () {
            // 1. 拿到元素:box,span
            let box = document.querySelector('.box')
            let span = box.lastElementChild
            console.log(box, span)
            // 2. 让box显示出来
            box.style.display = 'block'
            // 3. 开启定时器:span中的内容不断的减少到0
            let timeId = setInterval(function () {
                span.innerText--
                // 4. 到0:结束定时器,将广告隐藏
                if (span.innerText == 0) {
                    clearInterval(timeId)
                    box.style.display = ''
                }
            }, 1000)

        }, 3000)

        // 清理延时器
        clearTimeout(res)
    </script>
</body>

</html>

在这里插入图片描述



02-定时器-左右运动bug解决案例

    <style>
        * {
            margin: 0;
            padding: 0;
        }

        .box {
            position: absolute;
            left: 0;
            top: 100px;

            width: 100px;
            height: 100px;
            background-color: red;
        }
    </style>
</head>

<body>
    <button class="to-right">向右移动到600</button>
    <button class="to-left">向左移动到0</button>

    <div class="box"></div>

    <script>
        // 需求:点击向左移动到0,盒子移动到0(到了0要停止)

        // 获取到box元素
        let box = document.querySelector('.box')

        // 1. 给按钮to-right增加事件
        // 全局变量可以解决元素多个定时器的问题:有缺陷
        // 1. 如果页面必须有多个定时器就会出现问题:最终只有一个
        // 2. 全局变量可以被污染

        // 解决方案:将定时器存在元素自己身上(元素是一个对象,对象是引用传值:安全)
        // let timeId
        document.querySelector('.to-right').onclick = function () {
            // 先清除可能存在的定时器
            clearInterval(box.timeId)

            // 2. 事件处理:给box做动画,改变left的值,每次改变10px,到600结束
            box.timeId = setInterval(function () {
                console.log(1)
                box.style.left = box.offsetLeft + 11 + 'px'

                if (box.offsetLeft >= 600) {
                    // 注意:如果是匀速运动,要考虑移动可能会超出目标值:修正
                    box.style.left = 600 + 'px'
                    clearInterval(box.timeId)
                }
            }, 20)
        }

        // 1. 给按钮to-left增加事件
        document.querySelector('.to-left').onclick = function () {
            // 先清除可能存在的定时器
            clearInterval(box.timeId)
            // 2. 事件处理:给box做动画,改变left的值,每次改变10px,到0结束
            box.timeId = setInterval(function () {
                console.log(1)
                box.style.left = box.offsetLeft - 10 + 'px'

                if (box.offsetLeft <= 0) {
                    // 注意:如果是匀速运动,要考虑移动可能会超出目标值:修正
                    box.style.left = 0 + 'px'
                    clearInterval(box.timeId)
                }
            }, 20)
        }


        // box是一个盒子:元素对象
        box.index = 1           // 给box增加一个属性,值为1,非标准属性:点语法是不会进入元素本身(内存有效)
        console.log(box.index)
    </script>
</body>

</html>

注意:
// 1. 给按钮to-right增加事件
// 2.全局变量可以解决元素多个定时器的问题:有缺陷
// 3. 如果页面必须有多个定时器就会出现问题:最终只有一个
// 4. 全局变量可以被污染
// 解决方案:将定时器存在元素自己身上(元素是一个对象,对象是引用传值:安全)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在报错信息中,"at eval (webpack-internal:///./node_modules/webpack-dev-server/client/overlay.js:322:7)"是指错误发生的位置,具体是在webpack-dev-server的overlay.js文件的第322行的eval函数中。这个错误通常是由于观察元素的大小变化导致的循环问题引起的。为了解决这个问题,可以做以下操作: 1. 在app.vue或者main.js中添加以下代码段: ```javascript const debounce = (fn, delay) => { let timer = null; return function() { let context = this; let args = arguments; clearTimeout(timer); timer = setTimeout(function() { fn.apply(context, args); }, delay); } } const _ResizeObserver = window.ResizeObserver; window.ResizeObserver = class ResizeObserver extends _ResizeObserver { constructor(callback) { callback = debounce(callback, 16); super(callback); } } ``` 这段代码的作用是创建一个函数节流的工具函数,并修改ResizeObserver类的构造函数,使其使用函数节流来处理观察元素大小变化的回调函数。 通过以上操作,你可以解决报错信息中指出的位置的问题。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [handleError@webpack-internal:///./node_modules/webpack-dev-server](https://blog.csdn.net/weixin_46525113/article/details/130864747)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 33.333333333333336%"] - *2* [element-plus 报错 ResizeObserver loop limit exceeded 解决](https://blog.csdn.net/qq_45112637/article/details/131740110)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 33.333333333333336%"] - *3* [xhr-eval-chunk-webpack-plugin:通过XHR + eval加载块,而不是创建 elements](https://download.csdn.net/download/weixin_42106357/18703553)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 33.333333333333336%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值