forked from elving/wait
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwait.js
158 lines (125 loc) · 3.91 KB
/
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
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
(function () {
var root = this;
var toString = Object.prototype.toString;
var ids = {};
var isSeconds = /(seconds?|secs?|^([0-9]+)s)/i,
isMinutes = /(minutes?|mins?|^([0-9]+)m)/i,
isHours = /(hours?|hrs?|^([0-9]+)h)/i;
var isNumber = function (value) {
return toString.call(value) == '[object Number]';
};
var isString = function (value) {
return toString.call(value) == '[object String]';
};
var isBoolean = function (value) {
return toString.call(value) == '[object Boolean]';
};
var isArray = function (value) {
return toString.call(value) == '[object Array]';
};
var isTime = function (value) {
var timeTest = /(seconds?|secs?|^([0-9]+)s|minutes?|mins?|^([0-9]+)m|hours?|hrs?|^([0-9]+)h)/i;
return isNumber(value) || timeTest.test(value);
};
var parseTime = function (value) {
value = value.split(' ');
var number = 0,
unit = '';
if (value.length > 1) {
number = parseFloat(value[0]);
unit = value[1];
} else {
number = parseFloat(value);
if (isSeconds.test(value)) {
unit = 'seconds';
} else if (isMinutes.test(value)) {
unit = 'minutes';
} else if (isHours.test(value)) {
unit = 'hours';
}
}
if (isSeconds.test(unit)) {
return number * 1000;
} else if (isMinutes.test(unit)) {
return number * 60000;
} else if (isHours.test(unit)) {
return number * 3600000;
} else {
return number;
}
};
root.wait = function(time, callback, id) {
var timeout = 0;
time = isNumber(time) ? time : parseTime(time);
timeout = setTimeout(callback, time);
if (id && isString(id)) ids[id] = timeout;
return timeout;
};
root.repeat = function(time, callback, id, callBefore) {
var interval = 0;
time = isNumber(time) ? time : parseTime(time);
if (arguments.length === 3) {
if (isBoolean(id)) {
callBefore = id;
}
}
if (callBefore) callback();
interval = setInterval(callback, time);
if (id && isString(id)) ids[id] = interval;
return interval;
};
root.until = function(condition, callback, time, id) {
if (condition()) {
callback();
return;
}
if (arguments.length === 3) {
if (isTime(time) || isNumber(time)) {
time = time;
time = isNumber(time) ? time : parseTime(time);
} else {
id = time;
time = 100;
}
} else if (arguments.length === 4) {
time = time || 100;
time = isNumber(time) ? time : parseTime(time);
} else {
time = 100;
}
var interval = root.repeat(time, function() {
if (condition()) {
root.clear(interval);
callback();
}
}, id, false);
return interval;
};
_clear = function(id) {
clearInterval(id);
clearTimeout(id);
};
root.clear = function(id) {
var _ids = [],
_id = '';
if (arguments.length === 0) {
for(id in ids) {
_clear(ids[id]);
delete ids[id];
}
} else if (isArray(id) || isString(id)) {
_ids = isArray(id) ? id.slice() : [id];
for(var x = 0; x < _ids.length; x++) {
_id = _ids[x];
if (isString(_id)) {
_clear(ids[_id]);
delete ids[_id];
} else {
_clear(_id);
}
}
} else {
_clear(id);
}
};
})();