setInterval 和 clearInterval 的问题?

huangapple go评论67阅读模式
英文:

setInterval and clearInterval issues?

问题

I'm having issues with this timer I'm using to build a simple game. The function I'm trying to loop is function game(), which makes the div "box," initialized at the far right of the screen, animate all the way to the left of the screen, then immediately back to the far right. Essentially, it creates the illusion of obstacles coming towards the player. Clicking button "start" sets the interval. Clicking "stop" should clear it.

If I want to set the period of the interval, I know I have to change the second value in setInterval(game, x), but when I give a value x, only the first instance is delayed by that amount, then the function loops without delay. What is wrong with my function or the execution of setInterval? Do you have any suggestions for improving my method of accomplishing the above goal?

Also, my clearInterval(timer) only works sometimes, mostly when the period of the interval is long. Is there simply too much going on in the function and the program can't handle clearing the interval on a short period? Thanks for your help.

英文:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

$(&quot;#down&quot;).click(function(){ 
  $(&quot;#move&quot;).animate({top:&#39;+=75px&#39;}, 160, &#39;linear&#39;)
  });
$(&quot;#up&quot;).click(function(){ 
  $(&quot;#move&quot;).animate({top:&#39;-=75px&#39;}, 160, &#39;linear&#39;)
  });
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];
    $(&quot;#box&quot;).animate({right: &#39;+=100%&#39;}, 1000, &#39;linear&#39;);
    $(&quot;#box&quot;).animate({right: &#39;-=100%&#39;}, 10);
    $(&quot;#box&quot;).animate({top: c+&#39;%&#39;}, 0);    
};

var timer;
$(&quot;#start&quot;).on(&#39;click&#39;, function(){timer = setInterval(game,1000)})
$(&quot;#stop&quot;).on(&#39;click&#39;, function(){clearInterval(timer)})

<!-- language: lang-html -->

&lt;script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js&quot;&gt;&lt;/script&gt;
&lt;html&gt;
&lt;head&gt;
  &lt;style&gt;
    .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
    }
  &lt;/style&gt;
&lt;/head&gt;
&lt;body&gt;

&lt;div id = everything class = everything&gt;
  &lt;div class = move id = move&gt;&lt;/div&gt;
  &lt;div class = box id = box&gt;&lt;/div&gt;
  &lt;button id = start&gt;Start&lt;/button&gt;
  &lt;button id = stop&gt;Stop&lt;/button&gt;
  &lt;button id = down&gt;DOWN&lt;/button&gt;
  &lt;button id = up&gt;UP&lt;/button&gt;
&lt;/div&gt;

<!-- end snippet -->

I'm having issues with this timer I'm using to build a simple game. The function I'm trying to loop is function game(), which makes the div "box", initialized at the far right of the screen, animate all the way to the left of the screen, then immediately back to the far right. Essentially, it creates the illusion of obstacles coming towards the player. Clicking button "start" sets the interval. Clicking "stop" should clear it.

If I want to set the period of the interval, I know I have to change the second value in setInterval(game, x), but when I give a value x, only the first instance is delayed by that amount, then the function loops without delay. What is wrong with my function or the execution of setInterval? Do you have any suggestions for improving my method of accomplishing the above goal?

Also, my clearInterval(timer) only works sometimes, mostly when the period of the interval is long. Is there simply too much going on in the function and the program can't handle clearing the interval on a short period? Thanks for your help.

答案1

得分: 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);
});

HTML部分不需要翻译。

英文:

I would suggest to use requestAnimationFrame still find below answer for your issue

  1. Use $().finish() to stop animation with smaller interval.

  2. Also use clearInterval(timer) in your start as if you click start multiple times.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

$(&quot;#down&quot;).click(function(){ 
  $(&quot;#move&quot;).animate({top:&#39;+=75px&#39;}, 160, &#39;linear&#39;)
  });
$(&quot;#up&quot;).click(function(){ 
  $(&quot;#move&quot;).animate({top:&#39;-=75px&#39;}, 160, &#39;linear&#39;)
  });
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];
    $(&quot;#box&quot;).animate({right: &#39;+=100%&#39;}, 1000, &#39;linear&#39;);
    $(&quot;#box&quot;).animate({right: &#39;-=100%&#39;}, 10);
    $(&quot;#box&quot;).animate({top: c+&#39;%&#39;}, 0);    
};

var timer;
$(&quot;#start&quot;).on(&#39;click&#39;, function(){game();clearInterval(timer); timer = setInterval(game,1000)})
$(&quot;#stop&quot;).on(&#39;click&#39;, function(){
 $(&quot;#box&quot;).finish();
clearInterval(timer)
})

<!-- language: lang-html -->

&lt;script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js&quot;&gt;&lt;/script&gt;
&lt;html&gt;
&lt;head&gt;
  &lt;style&gt;
    .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
    }
  &lt;/style&gt;
&lt;/head&gt;
&lt;body&gt;

&lt;div id = everything class = everything&gt;
  &lt;div class = move id = move&gt;&lt;/div&gt;
  &lt;div class = box id = box&gt;&lt;/div&gt;
  &lt;button id = start&gt;Start&lt;/button&gt;
  &lt;button id = stop&gt;Stop&lt;/button&gt;
  &lt;button id = down&gt;DOWN&lt;/button&gt;
  &lt;button id = up&gt;UP&lt;/button&gt;
&lt;/div&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2020年1月3日 15:08:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/59574506.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定