-
Notifications
You must be signed in to change notification settings - Fork 7
/
example.html
75 lines (62 loc) · 1.57 KB
/
example.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<style type="text/css">
#foo{
width:100px;
height:50px;
background-color:blue;
display:none;
}
</style>
<script type="text/javascript" src="js/deferred.min.js"></script>
<script type="text/javascript">
// Original example from jQuery API documentation: http://api.jquery.com/deferred.promise/
// Create a Deferred and return its Promise
function asyncEvent(){
dfd = new Deferred().always(function(res) {
console.log("I'm always executed, this time it's a", res);
});
setTimeout(function(){
dfd.resolve("hurray");
}, Math.floor(Math.random()*1500));
setTimeout(function(){
dfd.reject("sorry");
}, Math.floor(Math.random()*1500));
return dfd.promise();
}
// Attach a done and fail handler for the asyncEvent
Deferred.when(asyncEvent()).then(
function(status){
console.log(status + ', things are going well');
},
function(status){
console.log(status + ', you fail this time');
}
);
// another example
function showDiv(){
dfd2 = new Deferred();
dfd2.done(function(){
document.getElementById('foo').style.display = 'block';
});
setTimeout(function() { dfd2.resolve() }, 2000);
return dfd2.promise();
}
showDiv();
var defer = Deferred().done(function(val) { alert(val); }),
filtered = defer.pipe(function( value ) {
return value * 2;
});
defer.resolve( 5 );
filtered.done(function( value ) {
alert( "Value is ( 2*5 = ) 10: " + value );
});
</script>
</head>
<body>
<div id="foo">
</div>
</body>
</html>