為什么我的關(guān)閉定時(shí)器不起效果
var timer=null;
function startMove(){
var divv=document.getElementById("div1");
setInterval(function(){
if (divv.offsetLeft==0) {
clearInterval(timer);
}else{
divv.style.left=divv.offsetLeft+10+'px';
}
},30)
}
var timer=null;
function startMove(){
var divv=document.getElementById("div1");
setInterval(function(){
if (divv.offsetLeft==0) {
clearInterval(timer);
}else{
divv.style.left=divv.offsetLeft+10+'px';
}
},30)
}
2017-02-24
舉報(bào)
2017-02-24
function startMove() {
????????????var divv = document.getElementById("div1");
? ? ? ? ? ? clearInterval(timer);//最開(kāi)始需要清空一下,否則以前執(zhí)行會(huì)留有緩存
? ? ? ? ? ? timer = setInterval(function() { //指定timer的值,否則要怎么清空
? ? ? ? ? ? ? ? if (divv.offsetLeft == 0) {
? ? ? ? ? ? ? ? ? ? clearInterval(timer);
? ? ? ? ? ? ? ? } else {
? ? ? ? ? ? ? ? ? ? divv.style.left = divv.offsetLeft + 10 + 'px';
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }, 30)
? ? ? ? }
//如果這樣還是解決不了問(wèn)題說(shuō)明css有問(wèn)題,是不是添加了border之類的屬性等等。
<!DOCTYPE html>
<html>
<head>
? ? <meta charset="UTF-8">
? ? <title>定時(shí)器</title>
? ? <style type="text/css">
? ? *{padding: 0;
? ? ?margin: 0}
? ? div {
? ? ? ? position: absolute;
? ? ? ? left: -200px;
? ? ? ? width: 400px;
? ? ? ? height: 50px;
? ? ? ? background: #59B24F;
? ? }
? ? </style>
? ? <script type="text/javascript">
? ? window.onload = function() {
? ? ? ? var divv = document.getElementById("div1");
? ? ? ? divv.onmouseover = function() {
? ? ? ? ? ? startMove();
? ? ? ? }
?? ? ? ?var timer = null;
? ? ? ? function startMove() {
? ? ? ? ? ? clearInterval(timer);
? ? ? ? ? ? timer = setInterval(function() {
? ? ? ? ? ? ? ? if (divv.offsetLeft == 0) {
? ? ? ? ? ? ? ? ? ? clearInterval(timer);
? ? ? ? ? ? ? ? } else {
? ? ? ? ? ? ? ? ? ? divv.style.left = divv.offsetLeft + 1 + 'px';
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }, 30)
? ? ? ? }
? ? }
? ? </script>
</head>
<body>
? ? <div id="div1"></div>
</body>
</html>