3
function DC() {
    var elements = $(".panel");
    elements.first().fadeOut(1000, function() {
        $(this).insertAfter(elements.last());
        $(this).fadeIn(1000);
    });
}

$(document).ready(function() {
    var i =0;
    for (var i=0; i < 10; i++) {
        DC();
    };
});

I want DC() to loop 10 times, but it only looped once.

What did I do wrong?

3
  • why do you create your var i 2 times? the for(var i = 0; i < 10; i++) should be enough to create your var i. To try find out if your loop really looped once, add an alert or so to check :) Commented Sep 28, 2011 at 9:24
  • typing mistake, sorry :) Commented Sep 28, 2011 at 9:30
  • Fixed your formatting for you. Please take a moment of care next time. Commented Sep 28, 2011 at 9:35

2 Answers 2

5

DC starts a fadeOut 10 times in a row. If you want your elements to fade out and back in 10 times then you should call DC as fadeIn's callback.

elements.first().fadeOut(1000, function() {
    $(this).insertAfter(elements.last());
    $(this).fadeIn(1000, DC);
});
Sign up to request clarification or add additional context in comments.

Comments

3

mpartel is correct in that you may want to call DC as a callback - but in order to ensure it doesn't run forever, you'll need to check against a count:

var count = 0;

function DC() {

    elements.first().fadeOut(1000, function() {
        if(count >= 10) return;
        count++;
        $(this).insertAfter(elements.last());
        $(this).fadeIn(1000, DC);
    });
}

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.