开发者

loop in javascript

function DC() {
    var elements = $(".panel");
    elements.first().fadeOut(1000, function() {
        $(this).insertAfte开发者_JS百科r(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?


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);
});


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);
    });
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜