-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample.html
124 lines (73 loc) · 2.42 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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
<!DOCTYPE html>
<html>
<head>
<title>AsyncJavascript jQuery Plugin Test Page</title>
<script src="//code.jquery.com/jquery-1.11.0.min.js"></script>
<script type="text/javascript" src="asyncjsloop.min.js"></script>
<script type="text/javascript">
$(function() {
//Very simple example!!
// --------------------- Recursion Example ---------------------
$("#startRecursion").click(function() {
startRecursion();
});
function startRecursion() {
$("#startRecursion").attr("disabled", true);
$.RecursiveCall(function(param1) {
var def = new $.Deferred();
//Simulate the async with a timeout.
setTimeout(function() {
$("#recursiveCallRes")
.append($("<p />").text(param1));
def.resolve(param1 + " processed!");
}, 500);
return def.promise();
},
[[0],[1],[5]])
.always(function (res) {
$("#startRecursion").removeAttr("disabled");
$("#recursiveCallRes")
.append($("<p />").text(JSON.stringify(res)));
});
}
// -------------------------------------------------------------
// ------------------------ Loop Example -----------------------
var asyncDiv = $("#loopRes");
var count = 0;
$("#startLoop").click(function() {
startLoop();
});
function startLoop() {
$.AsyncLoop(function mioloop() {
var def = new $.Deferred();
setTimeout(function() {
++count;
asyncDiv.append(" - " + count);
var stop = Math.floor(Math.random()*101) > 99; // 1% probability to stop.
if (stop) {
def.reject(count);
} else {
def.resolve(count);
}
}, 100);
return def.promise();
})
.done(function(res) {
asyncDiv.append(" - Ended with number: " + count + "; ");
});
}
// -------------------------------------------------------------
});
</script>
</head>
<body>
Discover the exaple by browsing the source code of this page!<br />
<input type="button" id="startRecursion" value="Start Recursive Call"></button>
<div id="recursiveCallRes" style="height: 130px; ">
</div>
<hr />
<input type="button" id="startLoop" value="Start Loop"></button>
<div id="loopRes" style="height: 120px; width: 500px; display:block;">
</div>
</body>
</html>