清除图库动画期间超时
我创建了一个具有自动播放功能的画廊。
当画廊停止播放动画并且我们将鼠标悬停在画廊上时,一切都会按预期进行。
但我遇到了一个奇怪的问题:
当画廊动画时 -->将鼠标悬停在画廊上时,clearTimeout 似乎无法正常工作,并且在 mouseleave 上,画廊表现得很奇怪
或者发生了我无法理解和解决的奇怪冲突。我回到几天前制作的另一个画廊,具有类似的功能,并且遇到了同样的问题。
我在这里遗漏了一些重要的东西
有问题的画廊:jsFiddle
var myTimeOut;
/////// ANIMATION /////////////
function animation(cb){
$('#slider').animate({left: '-=600' },800, cb);
}
/////// AUTO SLIDE ////////////
function auto() {
myTimeOut = setTimeout(function() {
animation(function(){
auto();
});
}, 2000);
}
auto();
以及我如何我正在尝试暂停它:
///// MOUSE actions //////////
$('#galcontainer').mouseenter( function () {
clearTimeout(myTimeOut);
});
$('#galcontainer').mouseleave( function () {
auto();
});
编辑
添加“悬停标志”(如答案中所建议的)效果几乎很好,但是当“在动画期间快速鼠标进入/鼠标离开”时,可以看到使用此解决方案的一个小错误。
解决该问题的方法可以是在 mouseleave 处添加 $('#slider').stop() 。这不是一个很好的解决方案。
我还能做什么?
I created a gallery with auto-play.
When the gallery STOP it's animation and we hover the gallery, all works as expected.
But I am getting a strange issue:
While the gallery is animating --> hovering the gallery, the clearTimeout seem not to work correctly and on mouseleave the gallery behave strangely
or it's happening a strange conflict I cannot understand and resolve. I went back to an other gallery I made a couple of days before fith a similar functionality and I encountered the same issue.
I am missing something crucial here
THE GALLERY IN QUESTION: jsFiddle
var myTimeOut;
/////// ANIMATION /////////////
function animation(cb){
$('#slider').animate({left: '-=600' },800, cb);
}
/////// AUTO SLIDE ////////////
function auto() {
myTimeOut = setTimeout(function() {
animation(function(){
auto();
});
}, 2000);
}
auto();
and how I'm trying to pause it:
///// MOUSE actions //////////
$('#galcontainer').mouseenter( function () {
clearTimeout(myTimeOut);
});
$('#galcontainer').mouseleave( function () {
auto();
});
EDIT
Adding a 'hover flag' (as suggested in the answers) works almost great, but a small bug using this solution is visible when 'fast mouseenter/mouseleave DURING the animation.
A fix to that issue could be adding a $('#slider').stop() at mouseleave. Not a great solution.
What else can I do?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
当动画正在进行时,有时不会触发 mouseout 事件,因此
pause
变量不会设置为 false。当触发下一个 mouseoever 事件时,因为暂停不是false
,它将再次设置超时。我只是确保在设置新的超时之前清除之前的所有超时。看看这个工作演示。
http://jsfiddle.net/8Lzxs/3/
When the animation is in progress sometimes the mouseout event is not getting triggered so
pause
variable is not set to false. when next mouseoever event is triggered since pause is notfalse
it will again set the timeout. I just made sure we clear any previous timeout before setting the new one.Take a look at this working demo.
http://jsfiddle.net/8Lzxs/3/
问题是在动画播放过程中,没有超时停止的情况。一个超时已经触发,另一个超时在动画完成之前不会创建。因此,如果您在动画进行时将鼠标悬停,动画将继续。您可以使用一个标志来解决这个问题,以判断动画是否应该停止。如果设置了该标志,则自动返回并且不会设置另一个超时。
http://jsfiddle.net/PXVSW/6/
The problem is that during the animation, there is no timeout to stop. The one timeout has already fired and other is not created until the animation completes. Because of this, if you hover when the animation is in progress, the animation continues. You can solve this with a flag to tell whether or not the animation should be stopped. If the flag is set, auto returns and does not set another timeout.
http://jsfiddle.net/PXVSW/6/
在没有深入研究代码的情况下,我通过添加一些额外的检查来修复它。
首先,我这样做是为了不用担心超时标识符被替换。我使用一个数组,以防多个事件堆叠在一起,并清除该数组中的所有超时。
其次,我添加了一个 stop() 值并使其在“stopped”为 true 时动画不会运行。
这是我的更改的小提琴: http://jsfiddle.net/6WhNw/
Without digging too deeply into your code, I fixed it by adding a couple of extra checks.
First, I made it so there's no worry about the timeOut identifier getting replaced. I use an array in case multiple events get stacked on top of each other, and clear all of the timeouts in that array.
Second, I added a stop() value and made it so the animation won't run if stopped is true.
Here's the fiddle with my changes: http://jsfiddle.net/6WhNw/
如果动画运行时鼠标进入,则不起作用。这是因为计时器已经启动,并且动画结束时将在完成时开始一个新的动画。我能想到的解决这个问题的最少重写是,当您将鼠标悬停在自动功能检查时设置一个全局标志,这样如果设置了该全局标志,它就不会启动新的动画。
您可以在此处查看
hoverFlag
解决方案:http://jsfiddle.net/jfriend00/EaKnj /。一个有趣的测试案例是让鼠标在动画运行时进入。
It doesn't work if your mouse enters while the animation is running. That's because the timer has already fired and the end of the animation is going to start a new one when it completes. The least rewrite I can think of to solve this would be to set a global flag when you're hovered that the auto function checks so it won't start a new animation if that global flag is set.
You can see the
hoverFlag
solution here: http://jsfiddle.net/jfriend00/EaKnj/.An interesting case to test is to make your mouse enter when the animation is running.
我发现 iOS 6 上的滚动(嗯,也是一种动画)似乎会导致同样的问题:只要视图滚动,超时就不会被可靠地清除。
我认为这是一个 iOS Bug,并且 - 就我而言 - 找到了一个不使用 set 和clearTimeout 的解决方案。
在(几个)Android Stock 浏览器和 Chrome 中无法重现相同的问题。
I found that scrolling (which is, well, a sort of animation too) on iOS 6 seems to cause the same problem: timeouts are not cleared reliably as long as a view scrolls.
I considered this an iOS Bug and - in my case - found a solution not using set and clearTimeout.
The same issue couldn't be reproduced in (several) Android Stock Browsers and Chrome.
您需要使用 setInterval 而不是 setTimeout。由于动画正在调用回调,因此它会一遍又一遍地调用动画。
http://jsfiddle.net/PXVSW/7/
You need to be using setInterval instead of setTimeout. Since the animation was calling the callback, it would call the animation over and over.
http://jsfiddle.net/PXVSW/7/
问题:在动画期间
mouseenter
上,jQuery.animate()
回调cb 已经在队列中(即使调用了clearTimeout,也会调用另一个
auto()
,这将触发后续运行等)。使用
setInterval
并利用所有这些回调是关键:jsFiddle 演示
(关于原始(尚未修改)代码的注释)
带有
"+="
和"0-"
的direction
变量是不需要的,因为只有一个方向,因此使用counter = 0
索引,并使用 Reminder%
运算符进行环绕/重置。另外,可以写成:
因为
auto
已经是一个函数。可以与
.hover(fn1, fn2)
方法一起使用(如上面的示例)The issue: on
mouseenter
during the animation, the jQuery.animate()
callbackcb
is already in queue (even if the clearTimeout was called, anotherauto()
will be called which will trigger subsequent runs, etc.).Using
setInterval
and leveraging all those callbacks is the key:jsFiddle demo
(Aside notes regarding the original (yet modified) code)
The
direction
variable with"+="
and"0-"
is unneeded since there's only one direction, so use acounter = 0
indexed, and wrap-around/reset using the Reminder%
operator. Also,can be written like:
since
auto
is already a function.can be used with
.hover(fn1, fn2)
method (like in the above example)