Warrantica
Warrantica

Reputation: 324

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?

Upvotes: 0

Views: 3455

Answers (2)

steveyang
steveyang

Reputation: 9298

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

Upvotes: 1

ground5hark
ground5hark

Reputation: 4514

You seem to be passing the function callback to setInterval incorrectly. Try this instead.

setInterval( animateMargin, 5000 );

Upvotes: 2

Related Questions