JavaScript定时器 发表于 2017-09-24 | 分类于 HTML 定时器的开启与关闭 12345678910111213141516171819202122232425262728293031323334353637<script>var timer=null;function startMove(){ clearInterval(timer); var oDiv=document.getElementById('div1'); timer=setInterval(function(){ if(oDiv.offsetLeft==0) { clearInterval(timer); } else { oDiv.style.left=oDiv.offsetLeft+10+'px'; } },30)}function startMove1(){ clearInterval(timer); var oDiv=document.getElementById('div1'); timer=setInterval(function(){ if(oDiv.offsetLeft==-200) { clearInterval(timer); } else { oDiv.style.left=oDiv.offsetLeft-10+'px'; } },30)}window.onload=function(){ var oDiv=document.getElementById('div1'); oDiv.onmouseover=function(){ startMove(); } oDiv.onmouseout=function(){ starMove1(); }</script>