2014-03-04 40 views
0

我想在.done()的所有內容完成後執行$(".foo").fadeIn("slow");如何在繼續執行.done()之前完成

現在,無論何時調用fadeIn,我仍然可以看到文本如何進行實時更改,因爲jQuery不會等到它完成。

我該怎麼做呢?

$(".notice").fadeOut("slow", function() { 
    $.ajax({ 
     url: "http://graph.facebook.com/name", 
     cache: false, 
     dataType: "JSON" 
    }) 
    .done(function(data) { 
     $(".foo .bar").text(data.name); 
    }); 
    $(".foo").fadeIn("slow"); // 
}); 
+0

是否有一個原因,爲什麼你不能把它放在'.done()'也是? –

回答

2

把你的代碼jQuery.done方法的回調內部

$(".notice").fadeOut("slow", function() { 
    $.ajax({ 
     url: "http://graph.facebook.com/name", 
     cache: false, 
     dataType: "JSON" 
    }) 
    .done(function(data) { 
     $(".foo .bar").text(data.name); 

     $(".foo").fadeIn("slow"); // 
    }); 

});

0

移動到你的Ajax的功能實現:

$(".notice").fadeOut("slow", function() { 
    $.ajax({ 
     url: "http://graph.facebook.com/name", 
     cache: false, 
     dataType: "JSON" 
    }) 
    .done(function(data) { 
     $(".foo .bar").text(data.name); 
     $(".foo").fadeIn("slow"); //move it here and it will be called if your ajax request is ready 
     callMethod(); //alternative you can call a mehtod 
    }); 
}); 

function callMethod() { 
    $(".foo").fadeIn("slow"); 
} 
0

您可以使用chain of done

$.ajax({ 
    url: "http://graph.facebook.com/name", 
    cache: false, 
    dataType: "JSON" 
}).done(function(data) { 
    $(".foo .bar").text(data.name); 
}).done(function() { 
    $(".foo").fadeIn("slow"); 
}); 

DEMO.

+0

你*可以*但有什麼意義? –

+0

這正是他所問:在第一次完成()後的所有指令後淡入。 – SirDeveloper

+0

我同意這就是他想要的,但這是矯枉過正。除非他沒有向我們顯示,否則''.done()'用於被緩存的對象或隊列。這裏情況不同。鏈接完成用於鏈接的異步調用。明智的表現是接受的答案是最好的,但當然,在其他情況下,你會更好。 –

相關問題