关于取消定时器——clearInterval
<style type="text/css"> *{margin: 0;padding: 0;} #box1{width: 200px;height:200px;position: relative;left: -200px;top: 0;background-color: red;} #box1 span{width: 20px;height: 50px;background-color: blue;position: absolute;left: 200px;top: 75px;} </style> <script> window.onload=function(){ var oDiv=document.getElementById("box1"); oDiv.onmouseover=function(){ starMove(); }; }; var timer=null; function starMove(){ clearInterval(timer); var oDiv=document.getElementById("box1"); timer=setInterval(function(){ if(oDiv.offsetLeft==0){ clearInterval(timer); } else { oDiv.style.left=oDiv.offsetLeft+1+"px"; } },30) }
没有设置*{margin: 0;padding: 0;}时为什么clearInterval(timer)没有作用