【问题标题】:setInterval and clearInterval issues?setInterval 和 clearInterval 问题?
【发布时间】:2020-01-03 16:50:53
【问题描述】:

$("#down").click(function(){ 
  $("#move").animate({top:'+=75px'}, 160, 'linear')
  });
$("#up").click(function(){ 
  $("#move").animate({top:'-=75px'}, 160, 'linear')
  });
function game(){
    var a = [0,10,15,20,25,35,40,45,50,55,62];
    var b = Math.floor(Math.random() * a.length);
    var c = a[b];
    $("#box").animate({right: '+=100%'}, 1000, 'linear');
    $("#box").animate({right: '-=100%'}, 10);
    $("#box").animate({top: c+'%'}, 0);    
};

var timer;
$("#start").on('click', function(){timer = setInterval(game,1000)})
$("#stop").on('click', function(){clearInterval(timer)})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<html>
<head>
  <style>
    .move {
      border: 1px solid black;
      position: absolute;
      left: 50px; top: 40%;
      height: 40px; width: 40px;
      background-color: rgb(0,255,100);      
    }
    .box {
      border: 1px solid black;
      position: absolute;
      right: 0%; top: 26%;
      height: 38%; width: 2.2%;
      background-color: red
    }
  </style>
</head>
<body>

<div id = everything class = everything>
  <div class = move id = move></div>
  <div class = box id = box></div>
  <button id = start>Start</button>
  <button id = stop>Stop</button>
  <button id = down>DOWN</button>
  <button id = up>UP</button>
</div>

我在使用这个计时器来构建一个简单的游戏时遇到了问题。我要循环的函数是function game(),它使在屏幕最右侧初始化的div“框”一直动画到屏幕左侧,然后立即回到最右侧。从本质上讲,它会产生障碍物向玩家靠近的错觉。单击“开始”按钮设置间隔。点击“停止”应该会清除它。

如果我想设置间隔的周期,我知道我必须更改setInterval(game, x) 中的第二个值,但是当我给一个值 x 时,只有第一个实例延迟了那个量,然后函数循环不延误。我的函数或 setInterval 的执行有什么问题?您对改进我实现上述目标的方法有什么建议吗?

另外,我的clearInterval(timer) 有时只能工作,主要是在间隔时间很长的时候。函数中是否发生了太多事情,并且程序无法在短时间内清除间隔?感谢您的帮助。

【问题讨论】:

  • 事件冒泡?您每 1000 毫秒触发一次,但也在游戏内$("#box").animate({right: '+=100%'}, 1000, 'linear');。
  • 感谢您的回复,马库斯泽勒。 “也在里面”是什么意思?
  • 能否添加html
  • 查看我上一条评论的引用行。动画也会延迟 1000 毫秒。
  • @chandukomati HTML 添加在上面。

标签: javascript jquery setinterval clearinterval


【解决方案1】:

我建议使用requestAnimationFrame 仍然可以为您的问题找到以下答案

1) 使用$().finish() 以更小的间隔停止动画。

2) 在您的start 中也使用clearInterval(timer),就像您多次单击开始一样。

$("#down").click(function(){ 
  $("#move").animate({top:'+=75px'}, 160, 'linear')
  });
$("#up").click(function(){ 
  $("#move").animate({top:'-=75px'}, 160, 'linear')
  });
function game(){
    var a = [0,10,15,20,25,35,40,45,50,55,62];
    var b = Math.floor(Math.random() * a.length);
    var c = a[b];
    $("#box").animate({right: '+=100%'}, 1000, 'linear');
    $("#box").animate({right: '-=100%'}, 10);
    $("#box").animate({top: c+'%'}, 0);    
};

var timer;
$("#start").on('click', function(){game();clearInterval(timer); timer = setInterval(game,1000)})
$("#stop").on('click', function(){
 $("#box").finish();
clearInterval(timer)
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<html>
<head>
  <style>
    .move {
      border: 1px solid black;
      position: absolute;
      left: 50px; top: 40%;
      height: 40px; width: 40px;
      background-color: rgb(0,255,100);      
    }
    .box {
      border: 1px solid black;
      position: absolute;
      right: 0%; top: 26%;
      height: 38%; width: 2.2%;
      background-color: red
    }
  </style>
</head>
<body>

<div id = everything class = everything>
  <div class = move id = move></div>
  <div class = box id = box></div>
  <button id = start>Start</button>
  <button id = stop>Stop</button>
  <button id = down>DOWN</button>
  <button id = up>UP</button>
</div>

【讨论】:

  • 如果你使用$().stop()你可以立即停止动画
  • 谢谢您,先生,您建议的 setInterval 方法效果很好!我在我的脚本中加入了你和 Zeller 对 requestAnimationFrame() 的反馈,这比 setInterval 好得多。感谢您的所有帮助!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-07-27
  • 2011-08-24
  • 2017-01-26
  • 2023-03-19
  • 2021-05-18
相关资源
最近更新 更多