-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwait.html
52 lines (42 loc) · 1.3 KB
/
wait.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
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>无标题文档</title>
</head>
<body>
$.wait jQuery extension that wraps setTimeout in a Deferred object.
Basic example:
$.wait(500).then(function(){
console.log('A half-second later.');
});
Passing the context as the 2nd parameter:
$.wait(500, someContext).then(function(){
this.something(); // "this" is the context parameter that you pass in
});
This utility is similar to _.delay, except that you can cancel the done callbacks by calling .reject() on the returned Deferred object:
var deferred = $.wait(5000);
deferred.done(function(){
console.log('This won\'t run if the deferred object is rejected within 5 seconds.');
});
if (confirm('Should we cancel the queued action?')) {
deferred.reject();
}
Use it along side the Promise-enhanced jqXHR object returned from $.ajax:
var jqXHR = $.ajax({
// ajax options here
});
$.when(jqXHR, $.wait(500)).done(function(){
console.log('This won\'t run until the AJAX has returned and at least 500 ms have passed.');
});
<script src="js/jquery1.91.min.js" type="text/javascript"></script>
<script src="js/asdui.js" type="text/javascript"></script>
<script>
$(function(){
$.wait(2000).then(function(){
console.log('A half-second later.');
});
});
</script>
</body>
</html>