<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>setInterval方法的使用</title>
    <style>
        div{
            width: 200px;
            height: 200px;
            background-color: red;
            position: absolute;
            top:50px;
        }
    </style>
</head>
<body>
<button>点我</button>
<div></div>
<script>
    function $(selector){
        return document.querySelector(selector);
    }
    var num = 0;
    $('button').onclick = function(){
        //开启定时器,占内存
        var Timer = setInterval(function(){
            if (num > 600){
                //停止定时器
                clearInterval(Timer)
            }
            num += 20;
            $('div').style.left = num + 'px';
        },100);
    };
</script>
</body>
</html>

 

相关文章:

  • 2022-12-23
  • 2023-03-19
  • 2022-03-06
  • 2021-10-16
  • 2022-12-23
  • 2022-12-23
  • 2021-12-03
  • 2021-08-25
猜你喜欢
  • 2022-12-23
  • 2022-01-29
  • 2022-12-23
  • 2022-12-23
  • 2022-01-07
  • 2021-05-19
  • 2021-09-21
相关资源
相似解决方案