$ .when.apply()中的警报错误消息

时间:2013-11-30 11:06:37

标签: jquery

我有一个jQuery函数$.when.apply($,fn).then(function(){ alert('success') });

当所有 fn []成功时,此alert('success')会触发。否则它不会触发成功警报。在这种情况下(当所有 fn []都不成功)时,我需要显示alert('error')。我怎样才能实现这个目标..?

2 个答案:

答案 0 :(得分:1)

您需要传递一个在Deferred被拒绝时调用的可选函数

$.when.apply($,fn).then(function(){ alert('success') }, function(){ alert('error') })

答案 1 :(得分:1)

您可以使用faildone代替

var promise = $.when.apply($,fn)
promise.done(function() { alert('done') });
promise.fail(function(){ alert('error') });

http://api.jquery.com/deferred.fail/