Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
490 views
in Technique[技术] by (71.8m points)

请问这个停止定时器为什么不工作?

JS新手,做了一个div无缝移动的代码,需要鼠标移入的时候清除定时器,可是鼠标移入后定时器为什么不停止?谢谢
<!DOCTYPE html>
<html lang="en">
<head>

<meta charset="UTF-8">
<title>Title</title>
<style>
     #container{width:200px;height:50px;background-color: rosybrown;position:relative;overflow:hidden;}
     #parent{width:400px;height:50px;background-color:red;position:absolute;}
    .children{width:46px;height:50px;display:inline-block;background-color:deepskyblue ;text-align:center;vertical-align:center; }

</style>
<script>

    window.onload=function() {

        var b = 0
        var c = setInterval(m, 80)
        function m() {
            var a = document.getElementById("parent")
            if (b >= -200) {
                b = b - 1
                a.style.left = b + "px"
            }
            else {
                b = 0

            }
        }

            var ochi = document.getElementById("parent")

            ochi.onmouseover = clearInterval(c)

            ochi.onmouseout = setInterval(m, 80)
    }
  • 列表项目

    </script>
    </head>

<body>
<div id="container">
<div id="parent">

<div class="children" >1</div>
<div class="children" >2</div>
<div class="children" >3</div>
<div class="children" >4</div>
<div class="children" >1</div>
<div class="children" >2</div>
<div class="children">3</div>
<div class="children">4</div>

</div>
</div>
</body>
</html>


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
ochi.onmouseover = function () {
    clearInterval(c)
}

ochi.onmouseout = function () {
    c = setInterval(m, 80)
}

注意onmouseout的时候要把时间函数负值给c


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...