forked from madbook/jquery.wait
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jquery.wait.js
89 lines (69 loc) · 2.85 KB
/
jquery.wait.js
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
/**
* jquery.wait - insert simple delays into your jquery method chains
* @author Matthew Lee matt@madleedesign.com
*/
(function ($) {
function jQueryDummy ($real, delay, _fncQueue) {
// A Fake jQuery-like object that allows us to resolve the entire jQuery
// method chain, pause, and resume execution later.
var dummy = this;
this._fncQueue = (typeof _fncQueue === 'undefined') ? [] : _fncQueue;
this._delayCompleted = false;
this._$real = $real;
if (typeof delay === 'number' && delay >= 0 && delay < Infinity)
this.timeoutKey = window.setTimeout(function () {
dummy._performDummyQueueActions();
}, delay);
else if (delay !== null && typeof delay === 'object' && typeof delay.promise === 'function')
delay.then(function () {
dummy._performDummyQueueActions();
});
else if (typeof delay === 'string')
$real.one(delay, function () {
dummy._performDummyQueueActions();
});
else
return $real;
}
jQueryDummy.prototype._addToQueue = function(fnc, arg){
// When dummy functions are called, the name of the function and
// arguments are put into a queue to execute later
this._fncQueue.unshift({ fnc: fnc, arg: arg });
if (this._delayCompleted)
return this._performDummyQueueActions();
else
return this;
};
jQueryDummy.prototype._performDummyQueueActions = function(){
// Start executing queued actions. If another `wait` is encountered,
// pass the remaining stack to a new jQueryDummy
this._delayCompleted = true;
var next;
while (this._fncQueue.length > 0) {
next = this._fncQueue.pop();
if (next.fnc === 'wait') {
next.arg.push(this._fncQueue);
return this._$real = this._$real[next.fnc].apply(this._$real, next.arg);
}
this._$real = this._$real[next.fnc].apply(this._$real, next.arg);
}
return this;
};
$.fn.wait = function(delay, _queue) {
// Creates dummy object that dequeues after a times delay OR promise
return new jQueryDummy(this, delay, _queue);
};
for (var fnc in $.fn) {
// Add shadow methods for all jQuery methods in existence. Will not
// shadow methods added to jQuery _after_ this!
// skip non-function properties or properties of Object.prototype
if (typeof $.fn[fnc] !== 'function' || !$.fn.hasOwnProperty(fnc))
continue;
jQueryDummy.prototype[fnc] = (function (fnc) {
return function(){
var arg = Array.prototype.slice.call(arguments);
return this._addToQueue(fnc, arg);
};
})(fnc);
}
})(jQuery);