为什么setInterval
在一段时间后运行得非常快?我用过代码
下面是我网站上的背景幻灯片。我也使用fullpage preloader plugin。
function slideSwitch() {
var $active = $('#slideshow IMG.active');
if ( $active.length == 0 ) $active = $('#slideshow IMG:last');
var $next = $active.next().length ? $active.next() : $('#slideshow IMG:first');
$active.addClass('last-active');
$next.css({opacity: 0.0})
.addClass('active')
.animate({opacity: 1.0}, 1000, function() {
$active.removeClass('active last-active');
});
}
$(function() {
setInterval( "slideSwitch()", 9999);
});
//use fullpage preloader
QueryLoader.init();
我想每9999毫秒重复一次动画。但经过一段时间(5-7分钟),动画似乎每1000毫秒重复一次。
答案 0 :(得分:1)
您可能希望确保一个动画不会以您不想要的方式中断前一个动画:
...
$next.css({ opacity: 0.0 })
.addClass('active')
.stop() // <-- new line
.animate({ opacity: 1.0}, 1000, function() {
$active.removeClass('active last-active');
});
答案 1 :(得分:0)
setInterval将每隔9999毫秒运行一次该函数,而不是一次。
setInterval将重复,setTimeout将运行一次。
您需要使用setTimeout。
答案 2 :(得分:0)
javascript函数setInterval()返回一个句柄
handle = setInterval("slideSwitch()", 9999);
您可以随时使用clearInterval
清除function killInterval(){
clearInterval(handle);
}
所以你需要在一段时间之后清理你的间隔,因为你想使用setTimeout
setTimeout("killInterval()",3000);