jquery fadein递归 - 如何检测完整和运行回调

时间:2013-06-11 16:34:54

标签: jquery

我需要在完成以下递归后运行回调:

function fade_day_div() { 
    $("div#day").first().fadeIn("slow", function showNext() {
        $(this).next("#day").fadeIn("fast", showNext);
    });
};

我可以在这里使用延迟对象吗?

2 个答案:

答案 0 :(得分:1)

这里不需要使用延迟对象,只需这样:

function fade_day_div() { 
  $("div#day").first().fadeIn("slow", function showNext() {
     var $next = $(this).next("#day");
     if($next.length > 0) {
         $next.fadeIn("fast", showNext);
     } else {
         ... your callback here ...
     }
  });
};

答案 1 :(得分:1)

promise()。done()可能就是这里的伎俩(尽管递归我不确定)。

function fade_day_div() { 
    $("div#day").first().fadeIn("slow", function showNext() {
        $(this).next("#day").fadeIn("fast", showNext).promise().done(function(){
               //Callback code here
        });
    });
};

可能会工作,可能不会。取决于如何嵌套递归。