简体   繁体   English

javascript中动画之间的延迟

[英]Delay between animation in javascript

I amm working on rotation of objects around a circle. 我正在围绕圆圈旋转物体。 I want to stop the animation when a box reachs at top, there should be a delay of seconds then start animation until next box reaches on top. 我想在一个盒子到达顶部时停止动画,应该有一个秒的延迟,然后开始动画,直到下一个盒子到达顶部。

Code Snippet: 代码片段:

if(x==40.109375 && y==218.015625){
  clearInterval(timer);
  timer = setInterval(animate, 1000);
}

x and y are the coordinates of top position xy是顶部位置的坐标

Fiddle 小提琴

到达红色圆圈位置时应停止

You have to set a timeout before the animation starts again. 您必须在动画再次开始之前设置超时。 Do it this way : 这样做:

setTimeout(function(){
    timer = setInterval(animate, 35);
 },1000);

As you mentioned it, weird things happen if the mouse enters/leaves the box many times. 正如你所提到的,如果鼠标多次进入/离开盒子会发生奇怪的事情。 To work around it, one solution would be to check the state of timer before changing it. 要解决它,一种解决方案是在更改timer之前检查timer的状态。 Please see this fiddle : 请看这个小提琴:

http://jsfiddle.net/p876D/3/ http://jsfiddle.net/p876D/3/

Or, as you have done it, clearing the timeout would work too 或者,正如您所做的那样,清除超时也会起作用

http://jsfiddle.net/p876D/4/ http://jsfiddle.net/p876D/4/

A timeout would delay the start of the next interval 超时会延迟下一个间隔的开始

if(x==40.109375 && y==218.015625){
    clearInterval(timer);
    setTimeout(function() {
        timer = setInterval(animate, 100);
    }, 1000);
}

FIDDLE 小提琴

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM