haoxiaolan 2022-05-25 22:58 采纳率: 34.7%
浏览 22
已结题

js定时器问题,接触定时器


function animate(obj, target, clink) {
    //清除以前的定时器,只保留一个定时器
    clearInterVal(obj.timer);
    obj.timer = setInterval(function () {
        if (obj.offsetLeft == target) {
            clearInterVal(obj.timer);
        }
        obj.style.left = obj.offsetLeft + 10 + 'px';
    }, 30)

    if (clink) {
        clink();
    }
}

<!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>Document</title>
    <style>
        div {
            position: relative;
            left: 0px;
            width: 300px;
            height: 300px;
            background-color: pink;
        }
    </style>

    <script src="./js/auto.js"></script>
</head>

<body>
    <button>123</button>
    <div></div>
    <script>
        var but = document.querySelector('button');
        var div = document.querySelector('div');
        but.onclick = function () {
            animate(div, 800);

        }
    </script>
</body>

</html>

img

这是什么原因?怎么解决啊

  • 写回答

1条回答 默认 最新

  • 你好!机器人 2022-05-25 23:06
    关注

    是clearInterval,你写错了

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 已结题 (查看结题原因) 7月12日
  • 已采纳回答 7月12日
  • 创建了问题 5月25日

悬赏问题

  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改