Javascript 如何等到.done()完成后再继续

Javascript 如何等到.done()完成后再继续,javascript,jquery,Javascript,Jquery,我想执行$(“.foo”).fadeIn(“慢”) 现在,无论何时调用fadeIn,我仍然可以看到文本是如何被实时更改的,因为jQuery不会等到它完成了 我该怎么做呢 $(".notice").fadeOut("slow", function() { $.ajax({ url: "http://graph.facebook.com/name", cache: false, dataType: "JSON" }) .done

我想执行
$(“.foo”).fadeIn(“慢”).done()
中的所有操作后,执行code>

现在,无论何时调用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"); //
});

将代码放入
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"); //
    });

}))

将其移动到ajax 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"); //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");
}
您可以使用:


你为什么不能把它也放在
.done()
中呢?这正是他所问的:在first done()中的所有说明之后,fadeIn。我同意这是他想要的,但那是过分的。除非有他没有给我们看的东西,
.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"); 
});