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

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

是clearInterval,你写错了