/
Timers.js
45 lines (39 loc) · 1.06 KB
/
Timers.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
var Timers = require('timers');
function hasRefresh() {
var testTimer = setTimeout(function() {}, 100);
var result = typeof testTimer.refresh === 'function';
clearTimeout(testTimer);
return result;
}
if (hasRefresh()) {
module.exports.setTimeout = setTimeout;
module.exports.clearTimeout = clearTimeout;
} else {
module.exports.setTimeout = function setTimeout(callback, after) {
if (typeof callback !== 'function') {
throw new TypeError('callback must be a function.');
}
var args = Array.prototype.slice.call(arguments, 2);
var timer = {
_idleNext : null,
_idlePrev : null,
_idleStart : null,
_idleTimeout : -1,
_repeat : null,
_onTimeout : function _onTimeout() {
callback.apply(this, args);
},
refresh: function refresh() {
Timers.active(this);
}
};
Timers.enroll(timer, after);
Timers.active(timer);
return timer;
};
module.exports.clearTimeout = function clearTimeout(timer) {
if (timer) {
Timers.unenroll(timer);
}
};
}