為什么我的定時器清楚不了???
<!DOCTYPE html>
<html lang="en">
<head>
? ?<meta charset="UTF-8">
? ?<title>Title</title>
? ?<style type="text/css">
? ? ? ?#div1{width: 200px;height: 200px;background:red;position: relative;left: -200px;}
? ? ? ?#div1 span{width: 20px;height: 50px;background: blue;position: absolute;left: 200px;top: 75px;}
? ?</style>
? ?<script type="text/javascript">
? ? ? ?window.onload=function(){
? ? ? ? ? ?var odiv=document.getElementById('div1');
? ? ? ? ? ?odiv.onmouseover=function(){
? ? ? ? ? ? ? ?startmove();
? ? ? ? ? ?}
? ? ? ?};
? ? ? ?var timer=null;
? ? ? ?function startmove(){
? ? ? ? ? ?var odiv=document.getElementById('div1');
? ? ? ? ? ?timer=setInterval(function(){
? ? ? ? ? ? ? ?if(odiv.offsetLeft==0){
? ? ? ? ? ? ? ? ? ?clearInterval(timer)
? ? ? ? ? ? ? ?}else{odiv.style.left=odiv.offsetLeft+10+'px'}
? ? ? ? ? ?},30);
? ? ? ?}
? ?</script>
</head>
<body>
<div id="div1">
? ?<span id="share">分享</span>
</div>
</body>
</html>
2016-06-03
在startMove函數(shù)執(zhí)行時,啟動清除所有的定時器.
function startMove(){
clearInterval(timer);
...
}
2016-05-29
在定時器啟動前也清除一遍,試試