2

I've got a bunch of blockquotes that I've managed to get to fade in one after another. At the moment, after the last one has faded in and out, the function ends. But I want it to loop and start from the beginning again. Here's my code so far that works:

$("div.quotes blockquote").each(function (index){
  $(this).delay(4500*index).fadeIn(500).delay(4000).fadeOut(500);
});

How do I get it to loop?

4 Answers 4

2

One possible solution:

function run() {
    var els = $("div.quotes blockquote");
    els.each(function(i) {
        $(this).delay(4500 * i).fadeIn(500).delay(4000).fadeOut(500, function() {
            if (i == els.length - 1) run();
        });
    });
}
run();

DEMO: http://jsfiddle.net/eDu6W/

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for all the answers but this is the one I used. It's one of the most simple solutions and does what I need
0
function toggleBlockQuotes()
{
    var countBlockquotes = $("div.quotes blockquote").length;
    var b = 1;
    $("div.quotes blockquote").each(function (index)
    {
        $(this).delay(4500*index).fadeIn(500).delay(4000).fadeOut(500);
        b++;
        if(b == countBlockquotes)
        {
            toggleBlockQuotes();
        }
    });
}

Take note that this will create an infinite loop.

Comments

0
function loop() {
    $("div.quotes blockquote").each(function(index) {
        $(this).delay(4500 * index).fadeIn(500).delay(4000).fadeOut(500, function() {
            loop();
        });
    });
}

loop();​

Comments

0

Could be done like this, depends what you need exactly: http://jsfiddle.net/MmPgG/

(function loopAnim() {
    var $elems = $("div")
    $elems.each(function(index) {
        var $that = $(this);
        (function() {
            $that.delay(4500 * index).fadeIn(500).delay(4000).fadeOut(500, function() {
                if (index == $elems.length - 1) {
                    $elems.show();
                    loopAnim()
                }
            });

        })($that)
    });
})()​

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.