jquery延期问题(ajax然后动画然后回调)

问题描述:

我必须做AJAX调用,做一些事后,并成为每everythig完成时的回调。jquery延期问题(ajax然后动画然后回调)

我打电话给getUsers()函数,并且需要知道什么时候这个函数中的所有步骤都是精确的。

没有我尝试作品...

function getUsers(userId){ 
    var dfd = $.Deferred(); 
    $.ajax({ 
     type: "GET", 
     url: "../getUsers.php", 
     data: {userid: userId}, 
     success: function(output){ 
      output = JSON.parse(output); 
      $(output.activeUsers).appendTo("#users"); 
      $(output.inactiveUsers).appendTo("#users_inactive"); 

      $('.user').each(function(index){ 
       $(this).animate({ 
        'height':55+'px' 
       }) 
      }) 
      $('.user').promise().done(function() { 
       overlay.fadeOut('fast',function(){ 
        dfd.resolve; 
        return dfd.promise(); 
       }); 
      }); 
     } 
    }) 
} 


$.when(
    getUsers() 
).then(function(){ 
    alert('something') 
}) 

我究竟做错了

+0

使用'完全被指出存在多个问题:''为$阿贾克斯()'。当请求完成时,这将会激发。请求是成功还是失败并不重要。 – Spokey 2014-09-05 13:15:48

+0

'getUsers'中缺少'return' – 2014-09-05 13:16:13

+0

Arun,在我需要进行回调的地方有返回,还是应该在最后?/Spokey,我需要一个不是在ajax之后的回调,而是所有的东西... – Sobakinet 2014-09-05 13:17:26

有在评论

function getUsers(userId) { 
    var dfd = $.Deferred(); 
    $.ajax({ 
     type: "GET", 
     url: "../getUsers.php", 
     data: { 
      userid: userId 
     }, 
     success: function (output) { 
      output = JSON.parse(output); 
      $(output.activeUsers).appendTo("#users"); 
      $(output.inactiveUsers).appendTo("#users_inactive"); 

      $('.user').animate({ 
       'height': 55 + 'px' 
      }).promise().done(function() { 
       overlay.fadeOut('fast', function() { 
        //need to invoke the resolve function 
        dfd.resolve(); 
       }); 
      }); 
     } 
    }).fail(function() { 
     //need to reject the promise if ajax request fails 
     dfd.reject(); 
    }); 
    //return the promise here 
    return dfd.promise(); 
} 


$.when(getUsers()).then(function() { 
    alert('something') 
})