$.when(array)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
var promises = [
ajaxs(1000).then(function(data){console.log(data)},function(data){console.log('reject:'+data)}),
ajaxs(2000).then(function(data){console.log(data)},function(data){console.log('reject:'+data)}),
ajaxs(3000).then(function(data){console.log(data)},function(data){console.log('reject:'+data)}),
ajaxs(4000).then(function(data){console.log(data)},function(data){console.log('reject:'+data)}),
ajaxs(4000).then(function(data){console.log(data)},function(data){console.log('reject:'+data)}),
ajaxs(3000).then(function(data){console.log(data)},function(data){console.log('reject:'+data)}),
ajaxs(2000).then(function(data){console.log(data)},function(data){console.log('reject:'+data)}),
ajaxs(5000).then(function(data){console.log(data)},function(data){console.log('reject:'+data)}),
ajaxs(1000).then(function(data){console.log(data)},function(data){console.log('reject:'+data)}),
ajaxs(6000).then(function(data){console.log(data)},function(data){console.log('reject:'+data)}),
ajaxs(3000).then(function(data){console.log(data)},function(data){console.log('reject:'+data)}),
];


$.when.apply($, promises).then(function(schemas) {
console.log("DONE", this, schemas);
}, function(e) {
console.log('FAIL');
});



var i=0;
function ajaxs(timeout) {
var dtd = $.Deferred();
setTimeout(function() {
if(i%2==0){
dtd.resolve(timeout);
}else{
dtd.reject(timeout);
}
i++;
}, timeout);
return dtd.promise();
}
打赏一瓶红牛吧,我好有力气继续加油!