• 防止定时器叠加的方法就是在开启定时器之前先清除定时器,再开启
  • 定时器叠加:开启定时器之后每次触发事件事件都会重复执行,表现为事件执行的频率加快
<!DOCTYPE html>
<html>
     <head>
           <meta charset="UTF-8">
           <title></title>
           <style>
                 #box{
                  width:150px;
                  height:150px;
                  background-color:red;
               }
           </style>
     </head>
     <body>
         <div ></div>
         <script>
               var box=document.getElementById("box");
               var change=0;
               var timer=null;
             //监听盒子的鼠标移入事件
     box.onmouseover=function(){
             //清除定时器
    clearInterval(timer);
             //开启定时器
            timer=setInterInterval(function(){
                 change+=1;
                consoel.log(change);
        },1000);    
          }
         </script>
     </body>
</html>

 

相关文章:

  • 2021-12-14
  • 2021-05-25
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-04-26
猜你喜欢
  • 2022-12-23
  • 2021-11-16
  • 2021-12-20
  • 2022-01-07
  • 2021-07-02
  • 2022-12-23
相关资源
相似解决方案