Why does setInterval
run very fast after some time? I used the code
below for a background slideshow on my 开发者_StackOverflow社区site. I also use the 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();
I want to repeat the animation every 9999 milliseconds. But after some time (5-7 minutes), the animation seems to repeat every 1000 milliseconds.
You might want to make sure that one animation doesn't interrupt the previous one in a way you're not intending:
...
$next.css({ opacity: 0.0 })
.addClass('active')
.stop() // <-- new line
.animate({ opacity: 1.0}, 1000, function() {
$active.removeClass('active last-active');
});
the javascript function setInterval() returns a handle
handle = setInterval("slideSwitch()", 9999);
that you can clear whenever you want with clearInterval
function killInterval(){
clearInterval(handle);
}
So you'd have to clean your interval after some time, for that you want to use setTimeout
setTimeout("killInterval()",3000);
setInterval will run the function every 9999 milliseconds, not once.
setInterval will repeat, setTimeout will run once.
You need to use setTimeout.
精彩评论