var id = setInterval(frame, 5);明明是声明,但是最后定时器会触发不明白为什么
html>
<html lang="en">
<html>
<style>
#container {
width: 400px;
height: 400px;
position: relative;
background: yellow;
}
#animate {
width: 50px;
height: 50px;
position: absolute;
background-color: red;
}
style>
<body>
<p><button onclick="myMove()">单击我button>
<div id ="container">
<div id ="animate">div>
div>
<script>
function myMove() {
var elem = document.getElementById("animate");
var pos = 0;
var id = setInterval(frame, 5);
- ****
function frame() {
if (pos == 350) {
clearInterval(id);
} else {
pos++;
elem.style.top = pos + "px";
elem.style.left = pos + "px";
}
}
}
script>
body>
html>
赋值了,也执行了。比如pop(),push()。声明了也执行了。这个和他的原因一样
很好理解 setInterval 好比是老鹰 他会飞(可直接执行的函数方法)它抓住一个老鼠 (var id) 一起飞上天了 导致老鼠也会飞了! 最终的结论是 抓谁 谁就会飞!
setInterval是函数,setInterval()是函数调用,调用后不就按照你定的时间进行触发