2011-08-29 51 views
2

背景如何防止JQuery使动画调用排队?

我对在很大程度上依赖于JQuery的.animate()的一个项目工作,偶尔通过Socket.io调用动画()接收外部更新。当窗口打开时,一切运行正常,动画呼叫可以异步运行。

问题

然而,当最小化浏览器或不同的选项卡打开,然后重新打开时,应同时关闭正在排队和运行中,他们收到已运行的所有动画。

这里是生命的呼唤:

$(div).animate({ 
    'top': this.topPos(), 
    'right': this.rightPos() 
}, 100); 

有没有一种简单的方法来对选项添加到动画()调用或调用一些jQuery函数或我应该只是添加相应的逻辑应用?

谢谢。

回答

4

使用.stop(true,true)通过传递true作为这两个参数

.stop([clearQueue], [jumpToEnd]) 

clearQueue: A Boolean indicating whether to remove queued animation as well. Defaults to false. 

jumpToEnd: A Boolean indicating whether to complete the current animation immediately. Defaults to false. 
在你的榜样

它将被应用,如:

$(div).stop(true,true).animate({ 
    'top': this.topPos(), 
    'right': this.rightPos() 
}, 100); 
+2

什么我建议 – vol7ron

+0

-1我很抱歉,但有用,因为.stop(真实的,真实的)是,作为@rick状态,其原因是jQuery的requestAnimationFrame是usesd那造成了这个问题。 – Tomgrohl

+0

对不起,这个答案是100%不正确的。 – Rick

1

在您再次拨打animate()之前,您可以在正在动画的元素上调用stop()。像这样:

$(div).stop().animate({ 
    'top': this.topPos(), 
    'right': this.rightPos() 
}, 100); 

它将停止正在排队的动画,并一次性触发。

here for details of stop()

希望这有助于。