forked from NativeScript/NativeScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtimer.ios.ts
More file actions
55 lines (42 loc) · 1.49 KB
/
timer.ios.ts
File metadata and controls
55 lines (42 loc) · 1.49 KB
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
/**
* iOS specific timer functions implementation.
*/
var timeoutCallbacks = {};
class TimerTargetImpl extends NSObject {
static new(): TimerTargetImpl {
return <TimerTargetImpl>super.new();
}
private _callback: Function;
public initWithCallback(callback: Function): TimerTargetImpl {
this._callback = callback;
return this;
}
public tick(timer): void {
this._callback();
}
public static ObjCExposedMethods = {
"tick": { returns: interop.types.void, params: [NSTimer] }
};
}
function createTimerAndGetId(callback: Function, milliseconds: number, shouldRepeat: boolean): number {
var id = new Date().getUTCMilliseconds();
var timerTarget = TimerTargetImpl.new().initWithCallback(callback);
var timer = NSTimer.scheduledTimerWithTimeIntervalTargetSelectorUserInfoRepeats(milliseconds / 1000, timerTarget, "tick", null, shouldRepeat);
if (!timeoutCallbacks[id]) {
timeoutCallbacks[id] = timer;
}
return id;
}
export function setTimeout(callback: Function, milliseconds = 0): number {
return createTimerAndGetId(callback, milliseconds, false);
}
export function clearTimeout(id: number): void {
if (timeoutCallbacks[id]) {
timeoutCallbacks[id].invalidate();
timeoutCallbacks[id] = null;
}
}
export function setInterval(callback: Function, milliseconds = 0): number {
return createTimerAndGetId(callback, milliseconds, true);
}
export var clearInterval = clearTimeout;