mirror of
https://github.com/NativeScript/NativeScript.git
synced 2025-08-16 03:31:45 +08:00
timer moved in separate module
This commit is contained in:
32
timer/timer.ios.ts
Normal file
32
timer/timer.ios.ts
Normal file
@ -0,0 +1,32 @@
|
||||
/**
|
||||
* 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: function (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: number): 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: number): number {
|
||||
return createTimerAndGetId(callback, milliseconds, true);
|
||||
}
|
Reference in New Issue
Block a user