javascript - 请问这个停止定时器为什么不工作?
本文介绍了javascript - 请问这个停止定时器为什么不工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
问 题
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>
解决方案
ochi.onmouseover = function () {
clearInterval(c)
}
ochi.onmouseout = function () {
c = setInterval(m, 80)
}
注意onmouseout
的时候要把时间函数负值给c
这篇关于javascript - 请问这个停止定时器为什么不工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文