-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtimer.js
61 lines (61 loc) · 1.59 KB
/
timer.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
(function() {
var clearTimer, timer;
timer = function(timeouts, interval, cb) {
var entry, setup, timeouts_i, timeouts_l;
if (cb === void 0) {
cb = interval;
interval = null;
}
if (typeof timeouts === 'number') {
timeouts = [timeouts];
}
timeouts_l = (timeouts != null ? timeouts.length : void 0) || 0;
timeouts_i = 0;
entry = {
clear: function() {
return clearTimer(entry);
}
};
(setup = function(call) {
if (call) {
cb();
}
if (timeouts_i < timeouts_l) {
return entry.timeout = setTimeout(setup, timeouts[timeouts_i++], true);
} else if (interval != null) {
delete entry.timeout;
return entry.interval = setInterval(cb, interval);
}
})();
return entry;
};
timer.auto = function(interval, cb) {
return timer(interval - Date.now() % interval, interval, cb);
};
timer.interval = function(interval, cb) {
return timer(null, interval, cb);
};
timer.clear = clearTimer = function(entry) {
if (entry != null ? entry.timeout : void 0) {
clearTimeout(entry.timeout);
delete entry.timeout;
}
if (entry != null ? entry.interval : void 0) {
clearInterval(entry.interval);
return delete entry.interval;
}
};
if (typeof exports !== "undefined" && exports !== null) {
exports.timer = timer;
}
if (typeof window !== "undefined" && window !== null) {
window.timer = timer;
}
if (typeof define === "function") {
define([], function() {
return {
timer: timer
};
});
}
}).call(this);