mirror of
https://github.com/NativeScript/NativeScript.git
synced 2025-08-15 19:26:42 +08:00
34 lines
1.1 KiB
TypeScript
34 lines
1.1 KiB
TypeScript
/**
|
|
* iOS specific timer functions implementation.
|
|
*/
|
|
var timeoutCallbacks = {};
|
|
|
|
function createTimerAndGetId(callback: Function, milliseconds: number, shouldRepeat: boolean): number {
|
|
var id = new Date().getUTCMilliseconds();
|
|
|
|
var target = Foundation.NSObject.extends({ tick: (timer) => { callback(); } }, { exposedMethods: { "tick:": "v@:@" } });
|
|
var timer = Foundation.NSTimer.scheduledTimerWithTimeIntervalTargetSelectorUserInfoRepeats(milliseconds / 1000, new target(), "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; |