setTimeout 通过多个选项卡加快速度
我遇到类似于 这个。但该解决方案对我没有帮助,因为我无法在文件中使用 php。
我的网站有一个滑块,其中包含每 8 秒移动一次的图像列表。但是,当我在浏览器中打开几个选项卡然后再次切换回来时,它就变得疯狂了。 滑块立即将图像一张一张地移动,没有 8 秒的时间延迟。
我只在 Chrome 和最新的 Firefox 中看到它。
**编辑:我检查了console.log(),setTimeout在clearTimeout之前和之后返回相同的数字。不知道为什么。也许这也有关系? **
编辑2:我添加了一个小提琴:http://jsfiddle.net/Rembrand/ qHGAq/8/
代码如下所示:
spotlight: {
i: 0,
timeOutSpotlight: null,
init: function()
{
$('#spotlight .controls a').click(function(e) {
// do stuff here to count and move images
// Don't follow the link
e.preventDefault();
// Clear timeout
clearTimeout(spotlight.timeOutSpotlight);
// Some stuff here to calculate next item
// Call next spotlight in 8 seconds
spotlight.timeOutSpotlight = setTimeout(function () {
spotlight.animate(spotlight.i);
}, 8000);
});
// Select first item
$('#spotlight .controls a.next:first').trigger('click');
},
animate: function(i)
{
$('#spotlight .controls li:eq(' + (spotlight.i) + ') a.next').trigger('click');
}
}
I’m having a setTimeout problem similar to this one. But that solution doesn't help me since I can’t use php in my file.
My site has a slider with a list of images that move every 8 seconds.However, when I have opened a few tabs in the browser and then switch back again, it goes nuts.
The slider proceeds to move the images one after the other immediately without the 8 second timedelay.
I'm only seeing it in Chrome and the latest Firefox.
**EDIT: I checked with console.log() and the setTimeout returns the same number before and after the clearTimeout. Not sure why. Maybe that also has something to do with it? **
EDIT 2: I added a fiddle: http://jsfiddle.net/Rembrand/qHGAq/8/
The code looks something like:
spotlight: {
i: 0,
timeOutSpotlight: null,
init: function()
{
$('#spotlight .controls a').click(function(e) {
// do stuff here to count and move images
// Don't follow the link
e.preventDefault();
// Clear timeout
clearTimeout(spotlight.timeOutSpotlight);
// Some stuff here to calculate next item
// Call next spotlight in 8 seconds
spotlight.timeOutSpotlight = setTimeout(function () {
spotlight.animate(spotlight.i);
}, 8000);
});
// Select first item
$('#spotlight .controls a.next:first').trigger('click');
},
animate: function(i)
{
$('#spotlight .controls li:eq(' + (spotlight.i) + ') a.next').trigger('click');
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
来自 jQuery 文档:
From the jQuery documentation:
我终于找到了答案,但这根本不是我所期待的。
罪魁祸首似乎是 jQuery 的 .animate(),我用它来移动滑块中的图像。
我用这个计算和移动我的图像位置:
现在的问题似乎是,在某些浏览器中,在切换到新选项卡并返回后,jQuery 的 .animate() 会保存动画并立即将它们全部触发。所以我添加了一个过滤器来防止排队。该解决方案来自 CSS-Tricks.com :
返回时看到的第一张幻灯片可以表现有点紧张,但比以前的超高速旋转木马要好。
此处修改完整代码
I finally found my answer and it’s not at all what I was expecting.
It seems the culprit is jQuery’s .animate(), which I use to move the images in the slider.
I calculate and move my images positions with this:
Now the problem seems to be that in some browsers, after you switch to a new tab and back, jQuery’s .animate() saves up the animations and fires them all at once. So I added a filter to prevent queueing. That solutions comes from CSS-Tricks.com :
The first slide you see when you go back can act a little jumpy but it’s better than the superspeed carousel from before.
Fiddle with the full code here
使用 jquery animate 队列属性有一种更简单的方法:
There is an easier way using the jquery animate queue property:
我不知道这是否对您有帮助,但它对我的幻灯片很有帮助。我所做的是,每次我调用一个由于 setTimeout 而应该在设定的时间间隔发生的动画时,我都会调用clearQueue(),这将消除已设置为发生的任何其他动画。然后我会调用动画。这样,当您返回该选项卡时,您就不会看到所有这些动画都排队,并且会变得疯狂。您最多只能设置一项。
所以像这样的事情:
它可能不适用于所有情况(取决于时间),但我希望这对某人有帮助!
I don't know if this will help you, but it helped me with my slideshow. What I did was everytime I called an animation that was supposed to happen at a set interval because of the setTimeout, I called clearQueue() which would get rid of any other animations that had been set to happen. then i'd call the animation. That way when you come back to that tab, you don't have all these animations queued up and it goes crazy. at max you'll only have one set up.
So something like this:
It may not work in all cases (depending on timing), but I hope that helps somebody!
您还必须认为您使用了clearTimeout。
当您调用 setTimeout 函数时,它会返回一个 ID,您可以将此 ID 保存在变量中,例如
,在设置新超时之前,您可以调用该函数,例如
You must also think you use clearTimeout.
As you call setTimeout function it returns an ID you can save this ID in a variable like
and before setting a new timeout you can call the function like
我怀疑浏览器会将“单击”等输入事件排队,但仅在事件发生的选项卡实际具有焦点时才触发它们。
也许您应该尝试直接调用点击回调,而不是使用
trigger('click')
。像这样的东西:
My suspicion is that the browser queues input events like 'click' but only fires them when the tab where the event occurs actually has focus.
Perhaps you should try calling your click callbacks directly instead of using
trigger('click')
.Something like this:
您运行的是哪个版本的 jQuery?显然这个问题在 1.6.3 版本中已经“修复”了——他们恢复了导致这种情况发生的更改。讨论此处和此处。
尽管这个问题将来可能必须得到解决,但目前看来我们已经摆脱了困境。
What version of jQuery are you running? Apparently this problem was 'fixed' for version 1.6.3 - they reverted the change that caused this to happen. Discussions here and here.
Though this issue will likely have to be addressed in the future, it seems as though we're off the hook for now.