clearInterval無效?
把onmouseover換成onclick時(shí),第一個(gè)clearInterval(timer)就不能起到作用了,有沒有同學(xué)遇到相同的情況??
<body>
<div?id="container">
<div?id="box"></div>
</div>
<script?type="text/javascript">
window.onload=function(){
var?obox=document.getElementById("box");
obox.onclick =function(){
startmove();
}
}
function?startmove(){
var?timer=null;
clearInterval(timer);
var?oc=document.getElementById("container");
setInterval(function(){
if?(oc.offsetLeft==0)?{
clearInterval(timer);
}
else{
oc.style.left=oc.offsetLeft+1+"px";
}
},30)
}
</script>
</body>
2016-06-21
1、var timer=null應(yīng)該聲明在function函數(shù)外面。若聲明在函數(shù)內(nèi)部,每次調(diào)用函數(shù)時(shí)都會(huì)在函數(shù)內(nèi)部聲明一個(gè)timer變量,且每個(gè)timer變量都不相同,所以clearImterval(timer)不起作用。與換成onclick沒有關(guān)系。
2、timer=setInterval().
2016-07-05
第18行應(yīng)該是timer=setInterval(function(){ ? ? ? ?吧。
2016-06-23
樣式上加上 body{margin:0;padding:0;}?
要不然
oc.style.left=oc.offsetLeft+1+"px";這語句只會(huì)是每次加9