Clinton Green
Clinton Green

Reputation: 9977

jQuery setInterval not looping

I have a function that I want to loop. I found the easiest way to do this was using setInterval. When I tried this the function only runs once. Please advise where I am going wrong. Cheers

function empMove() { $('.emp-wrap').css('margin-top', '-100px')};

setInterval(empMove, 2000);

I have a div with multiple rows and I want to show only one at a time, hence I am decreasing the margin-top each time.

Upvotes: 0

Views: 311

Answers (3)

Hugolpz
Hugolpz

Reputation: 18278

function displayOnly() {
    var initHeight = $('.wrapper').height();
    $('.wrapper').height(initHeight);
    $('p').not('.read').fadeOut(2000, function () {
        if ($(this).is(':last-child')) {
            $('.wrapper').animate({
                height: $('.inner').height()
            }, 2000);
        }
    });
}

$("button").click(displayOnly);

In action on fiddle

Upvotes: 0

mate64
mate64

Reputation: 10082

I highly recommend the jQuery timing plugin (2KB) (GitHub Repository, Docs).

It provides easy-to-use loop animations and much more. Have a look:

function empMove() { 

    $('.emp-wrap').css('margin-top', '-=100px').repeat().wait(2000);
};

Upvotes: 1

TwiN
TwiN

Reputation: 1911

The current code sets the top margin to -100px. Try

function empMove() { $('.emp-wrap').css('margin-top', '-=100')};

Upvotes: 5

Related Questions