mirror of
https://github.com/NativeScript/NativeScript.git
synced 2025-08-17 21:01:34 +08:00
clearTimeout, setInterval, clearInterval for iOS
This commit is contained in:
@ -1,7 +1,32 @@
|
|||||||
/**
|
/**
|
||||||
* iOS specific global functions implementation.
|
* iOS specific global functions implementation.
|
||||||
*/
|
*/
|
||||||
export function setTimeout(callback: Function, milliseconds: number): void {
|
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 target = Foundation.NSObject.extends({ tick: function (timer) { callback(); } }, { exposedMethods: { "tick:": "v@:@" } });
|
||||||
Foundation.NSTimer.scheduledTimerWithTimeIntervalTargetSelectorUserInfoRepeats(milliseconds / 1000, new target(), "tick:", null, false);
|
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