我有一个jQuery函数$.when.apply($,fn).then(function(){ alert('success') });
当所有 fn []成功时,此alert('success')
会触发。否则它不会触发成功警报。在这种情况下(当所有 fn []都不成功)时,我需要显示alert('error')
。我怎样才能实现这个目标..?
答案 0 :(得分:1)
您需要传递一个在Deferred被拒绝时调用的可选函数
$.when.apply($,fn).then(function(){ alert('success') }, function(){ alert('error') })
答案 1 :(得分:1)
您可以使用fail
和done
代替
var promise = $.when.apply($,fn)
promise.done(function() { alert('done') });
promise.fail(function(){ alert('error') });