-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtimeout.js
62 lines (59 loc) · 1.17 KB
/
timeout.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
/* globals coscript */
var fibers = [];
var setTimeout = function(
func,
delay,
param1,
param2,
param3,
param4,
param5,
param6,
param7,
param8,
param9,
param10
) {
// fibers takes care of keeping coscript around
var id = fibers.length;
fibers.push(
coscript.scheduleWithInterval_jsFunction((delay || 0) / 1000, function() {
try {
func(
param1,
param2,
param3,
param4,
param5,
param6,
param7,
param8,
param9,
param10
);
} catch (err) {
if (
typeof process !== "undefined" &&
process.listenerCount &&
process.listenerCount("uncaughtException")
) {
process.emit("uncaughtException", err, "uncaughtException");
} else {
throw err;
}
}
})
);
return id;
};
var clearTimeout = function(id) {
var timeout = fibers[id];
if (timeout) {
timeout.cancel(); // fibers takes care of keeping coscript around
fibers[id] = undefined; // garbage collect the fiber
}
};
module.exports = {
setTimeout: setTimeout,
clearTimeout: clearTimeout
};