jQuery/JavaScript:当选项卡变为非活动状态时,我的递归 setTimeout 函数会加速
在我正在构建的这个 jQuery 幻灯片插件中,我遇到了一个奇怪的小困境。
这没什么花哨的,我迄今为止编写的代码运行得很好,但是我注意到,当我让网站运行并切换到新选项卡并继续在另一个选项卡中浏览网页时(在我的例子中是 Chrome for Mac) ) 当我返回站点时,setTimeout
调用似乎加快了速度,并且不是等待计时器完成触发事件,而是连续触发。
这是我的(简化的)代码:
var timer;
var counter;
var slides; // collection of all targeted slides.
// animate to the next slide
function nextSlide() {
// stop timer
methods.stopTimer();
// increase counter
counter++;
if ( counter > slides.length-1 ) { counter = 0; } // if counter is greater than the amount of slides, back to the start.
// inner = container to be animated
// in the complete callback restart the timer.
inner.animate({
'left': '-'+slides.eq( counter ).position().left
}, {
duration : settings.animationSpeed,
easing : 'easeInOutExpo',
complete : startTimer()
});
}
// timer functions.
function startTimer() {
if ( timer === '' ) {
timer = setTimeout( function() {
nextSlide();
} , 3000 );
}
}
function stopTimer() {
clearTimeout( timer );
timer = '';
}
所以应该发生的是,在动画结束时,计时器重新连接另一个 setTimeout
调用,以便它成为连续的幻灯片(在您离开选项卡之前,这一切都很好。
一旦您离开选项卡并返回带有幻灯片的选项卡,3000 ms
计时器似乎已减少到立即调用,现在是动画的时刻完成下一项立即开始 任何关于
为什么会发生这种情况以及如何解决的想法都将非常感谢
您的阅读,
Jannis 。
I've got an odd little dilemma in this jQuery slideshow plugin that I am building.
It's nothing fancy and the code I have written to date is working great however I have noticed that when I leave the site running and switch to a new tab and continue browsing the web in this other tab (Chrome for Mac in my case) that when I return to my site, the setTimeout
call seems to have speed up and instead of waiting for the timer to finish the fire the event, it fires continuously.
Here is my (simplified) code:
var timer;
var counter;
var slides; // collection of all targeted slides.
// animate to the next slide
function nextSlide() {
// stop timer
methods.stopTimer();
// increase counter
counter++;
if ( counter > slides.length-1 ) { counter = 0; } // if counter is greater than the amount of slides, back to the start.
// inner = container to be animated
// in the complete callback restart the timer.
inner.animate({
'left': '-'+slides.eq( counter ).position().left
}, {
duration : settings.animationSpeed,
easing : 'easeInOutExpo',
complete : startTimer()
});
}
// timer functions.
function startTimer() {
if ( timer === '' ) {
timer = setTimeout( function() {
nextSlide();
} , 3000 );
}
}
function stopTimer() {
clearTimeout( timer );
timer = '';
}
So what should happen is that at the end of the animation, the timer gets reattached with another setTimeout
call so that it becomes a continuous slideshow (and this works just fine until you leave the tab.
Once you leave the tab and return to the tab with slideshow it seems that the 3000 ms
timer has been reduced to invoke instantly and now the moment the animation finishes the next one starts with no delay at all.
Any ideas on why this is happening on how it can be solved would be much appreciated.
Thanks for reading,
Jannis
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
当选项卡变为非活动状态时,某些浏览器(如 Chrome)会大大减慢循环计时器的速度,然后,当选项卡再次活动时,它们会尝试“赶上”,以便发生相同数量的实际计时器事件。我能想到的解决方法是,当选项卡变为非活动状态时完全停止幻灯片放映,并在选项卡变为活动状态时重新启动幻灯片。
Some browsers (like Chrome) drastically slow down recurring timers when the tab goes inactive and then, when the tab comes active again, they try to "catch up" so that the same number of actual timer events has occurred. All I can think of as a work-around is for you to stop the slideshow entirely when the tab goes inactive and start it again when it comes active.
您是否尝试过间隔而不是超时?另外,为了使其递归,只需调用 nextSlide() 函数作为其自己的回调:
然后只需启动和停止一个间隔即可:
Instead of timeouts have you tried intervals? Also for it to be recursive, just call the nextSlide() function as its own callback:
Then it's just a matter of starting and stopping an interval: