關(guān)于取消定時器——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) ????}
沒有設(shè)置*{margin: 0;padding: 0;}時為什么clearInterval(timer)沒有作用
2016-05-21
我寫的是對的,我的意思是去掉*{margin:0;padding:0}為什么會導致定時器取消不起作用
2016-05-21
我用你的代碼測試了下是可以的