-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
48 lines (48 loc) · 1.64 KB
/
index.html
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
37
38
39
40
41
42
43
44
45
46
47
48
<!DOCTYPE HTML>
<html>
<body>
<div type="1">df1,df2都失败</div>
<div type="2">df1,df2都成功</div>
<div type="3">df1,df2有一个成功</div>
<script src="http://code.jquery.com/jquery-1.7.1.js"></script>
<script src="jquery.whether.js"></script>
<script type="text/javascript">
function test(type){
var df1 = $.Deferred();
var df2 = $.Deferred();
df2.done(function(){
console.log('df2 success');
}).fail(function(){
console.log('df2 failed');
});
df1.done(function(){
console.log('df1 success');
}).fail(function(){
console.log('df1 failed');
});
var df = $.whether(df1,df2).then(function(data){
console.log(data);
console.log('df success,at least one defer succeed');
}).fail(function(data1,data2){
console.log(data1);
console.log(data2);
console.log('all failed!');
});
if(type == 1){
df1.rejectWith(df1,[{ajaxdata:'fail data from df1'}]);
df2.rejectWith(df2,[{ajaxdata:'fail data from df2'}]);//all failed
}else if(type == 2){
df1.resolveWith(df1,[{ajaxdata:'data from df1'}]);// at least on defer succeed
df2.resolveWith(df2,[{ajaxdata:'data from df2'}]);// at least on defer succeed
}else if(type == 3){
df1.rejectWith(df1,[{ajaxdata:'fail data from df1'}]);
df2.resolveWith(df2,[{ajaxdata:'data from df2'}]);// at least on defer succeed
}
}
$('div').click(function(){
var type = $(this).attr('type');
test(type);
});
</script>
</body>
</html>