js计时器clearInterval的清除
如何用clearInterval清除几个计时器啊,clearInterval(timer1,timer2,timer3,timer4,);为什么不对啊?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
如何用clearInterval清除几个计时器啊,clearInterval(timer1,timer2,timer3,timer4,);为什么不对啊?
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(8)
没能解决按多次小方块速度不变的问题,因为当按一次向下 的键小方块向下走一步,这时direction = "xia"了,从而if(direction == "xia"){return false; },当计时器timer1 = setInterval("xia()",500)再调用这个函数时因为direction = "xia"所以不执行了。还是要谢谢你的回答。
非常感谢
<body>
<div id="desktop" style="border:1px solid #000; width:500px; height:500px; margin:0 auto; position:relative;">
<div id="object" style="position:absolute; top:10px; left:10px; width:10px; height:10px; background-color:#F00;"></div>
</div>
<script type="text/javascript">
var timer1,timer2,timer3,timer4;
var direction = null;
function xia(){
if(direction == "xia"){return false; }
document.getElementById("object").style.top = (parseInt(document.getElementById("object").style.top)+ 10) + "px";
if(parseInt(document.getElementById("object").style.top) > 490){
document.getElementById("object").style.top = 490 + "px";
}
clearTimeout(timer2);
clearTimeout(timer3);
clearTimeout(timer4);
timer1 = setTimeout("xia()",5000);
direction = "xia";
}
function shang(){
//if(direction == 'shang') return false;
//direction = 'shang';
document.getElementById("object").style.top = (parseInt(document.getElementById("object").style.top)- 10) + "px";
if(parseInt(document.getElementById("object").style.top) <0){
document.getElementById("object").style.top = 0 + "px";
}
}
function zuo(){
//if(direction == 'zuo') return false;
//direction = 'zuo';
document.getElementById("object").style.left = (parseInt(document.getElementById("object").style.left)- 10) + "px";
if(parseInt(document.getElementById("object").style.left) <0){
document.getElementById("object").style.left = 0 + "px";
}
}
function you(){
//if(direction == 'you') return false;
//direction = 'you';
document.getElementById("object").style.left = (parseInt(document.getElementById("object").style.left)+ 10) + "px";
if(parseInt(document.getElementById("object").style.left) > 490){
document.getElementById("object").style.left = 490 + "px";
}
}
document.onkeydown = function(e){
var tops = document.getElementById("object").style.top;
var e = e || window.event;
switch(e.keyCode){
case 40:
xia();
break;
case 38:
clearInterval(timer1);
clearInterval(timer3);
clearInterval(timer4);
timer2 = setInterval("shang()",500);
break;
case 37:
clearInterval(timer2);
clearInterval(timer1);
clearInterval(timer4);
timer3 = setInterval("zuo()",500);
break;
case 39:
clearInterval(timer2);
clearInterval(timer3);
clearInterval(timer1);
timer4 = setInterval("you()",500);
break;
default:
document.write("I'm looking forward to this weekend!")
}
};
</script>
</body>完整代码如上,请高手指点。
整个页面是这样的 <body> <div id="desktop" style="border:1px solid #000; width:500px; height:500px; margin:0 auto; position:relative;"> <div id="object" style="position:absolute; top:10px; left:10px; width:10px; height:10px; background-color:#F00;"></div> </div> <script type="text/javascript"> var timer1,timer2,timer3,timer4; var direction = null; function xia(){ if(direction == "xia"){return false; } document.getElementById("object").style.top = (parseInt(document.getElementById("object").style.top)+ 10) + "px"; if(parseInt(document.getElementById("object").style.top) > 490){ document.getElementById("object").style.top = 490 + "px"; } clearTimeout(timer2); clearTimeout(timer3); clearTimeout(timer4); timer1 = setTimeout("xia()",500); direction = "xia"; } function shang(){ if(direction == 'shang') return false; direction = 'shang'; document.getElementById("object").style.top = (parseInt(document.getElementById("object").style.top)- 10) + "px"; if(parseInt(document.getElementById("object").style.top) <0){ document.getElementById("object").style.top = 0 + "px"; } } function zuo(){ if(direction == 'zuo') return false; direction = 'zuo'; document.getElementById("object").style.left = (parseInt(document.getElementById("object").style.left)- 10) + "px"; if(parseInt(document.getElementById("object").style.left) <0){ document.getElementById("object").style.left = 0 + "px"; } } function you(){ if(direction == 'you') return false; direction = 'you'; document.getElementById("object").style.left = (parseInt(document.getElementById("object").style.left)+ 10) + "px"; if(parseInt(document.getElementById("object").style.left) > 490){ document.getElementById("object").style.left = 490 + "px"; } } document.onkeydown = function(e){ var tops = document.getElementById("object").style.top; var e = e || window.event; switch(e.keyCode){ case 40: xia(); break; case 38: clearInterval(timer1); clearInterval(timer3); clearInterval(timer4); timer2 = setInterval("shang()",500); break; case 37: clearInterval(timer2); clearInterval(timer1); clearInterval(timer4); timer3 = setInterval("zuo()",500); break; case 39: clearInterval(timer2); clearInterval(timer3); clearInterval(timer1); timer4 = setInterval("you()",500); break; default: document.write("I'm looking forward to this weekend!") } }; </script> </body> 请高手指点。
改成
定义一个全局方向标志,例如 var direction = null;
麻烦高手看看,问题出在clearInterval(timer2);clearInterval(timer3);clearInterval(timer4);当运行xia()时应该把其他的计时器清除,可是实际上还是没清除了。还有就是当按了一下向下的键时,下方块向下运动,当再按一下时,小方块会加快速度,如何才能保证按了多下小方块的速度不变啊
function xia(){
document.getElementById("object").style.top = (parseInt(document.getElementById("object").style.top)+ 10) + "px";
if(parseInt(document.getElementById("object").style.top) > 490){
document.getElementById("object").style.top = 490 + "px";
}
}
function shang(){
document.getElementById("object").style.top = (parseInt(document.getElementById("object").style.top)- 10) + "px";
if(parseInt(document.getElementById("object").style.top) <0){
document.getElementById("object").style.top = 0 + "px";
}
}
function zuo(){
document.getElementById("object").style.left = (parseInt(document.getElementById("object").style.left)- 10) + "px";
if(parseInt(document.getElementById("object").style.left) <0){
document.getElementById("object").style.left = 0 + "px";
}
}
function you(){
document.getElementById("object").style.left = (parseInt(document.getElementById("object").style.left)+ 10) + "px";
if(parseInt(document.getElementById("object").style.left) > 490){
document.getElementById("object").style.left = 490 + "px";
}
}
document.onkeydown = function(e){
var timer1,timer2,timer3,timer4;
var tops = document.getElementById("object").style.top;
var e = e || window.event;
switch(e.keyCode){
case 40:
timer1 = setInterval("xia()",500);
clearInterval(timer2);
clearInterval(timer3);
clearInterval(timer4);
break;
case 38:
timer2 = setInterval("shang()",500);
clearInterval(timer1);
clearInterval(timer3);
clearInterval(timer4);
break;
case 37:
timer3 = setInterval("zuo()",500);
clearInterval(timer2);
clearInterval(timer1);
clearInterval(timer4);
break;
case 39:
timer4 = setInterval("you()",500);
clearInterval(timer2);
clearInterval(timer3);
clearInterval(timer1);
break;
default:
document.write("I'm looking forward to this weekend!")
}
};
这是我写的函数,想写个类似贪吃蛇的游戏,按键盘上的向上的方向键,小方块想上走,按向下的向下走,可是我连按这两个方向键时,它即向上又向下,怎么办呢?
这个函数只接受一个参数clearInterval(id_of_setinterval)
你可用通过多次调用,来清理