jQuery: setInterval animations?
Currently I'm trying to make some sort of vertical auto-scrolling. This is my code
$(document).ready(function() {
var reachEnd = false;
var top = 0;
function animateMargin(){
if(top == -720){
reachEnd = true;
}
if(reachEnd == false){
$('#bslider').animate({'marginTop' : '-=240px'}, 500);
top -=240;
}else{
$('#bslider').animate({'marginTop' : '0px'}, 1000);
top = 0;
reachEnd = false;
}
};
marginInterval = setInterval('animateMargin()', 5000);
$('#banner').hover(function(){
clearInterval( marginInterval );
},
function(){
marginInterval = setInterval('animateMargin()', 5000);
});
});
And it's not working 开发者_高级运维- at all.
Any ideas?
You seem to be passing the function
callback to setInterval
incorrectly. Try this instead.
setInterval( animateMargin, 5000 );
setInterval()
takes an callback function.
Passing animateMargin()
with (), you have passed in the result of the function call, which is the return value of the function.
Instead, you should pass in the function itself as animateMargin
精彩评论