mirror of
https://github.com/NativeScript/NativeScript.git
synced 2025-11-05 13:26:48 +08:00
Fix timer id generation -- we shouldn't have clashing id's anymore.
This commit is contained in:
@@ -3,24 +3,26 @@
|
||||
*/
|
||||
var timeoutHandler;
|
||||
var timeoutCallbacks = {};
|
||||
var timerId = 0;
|
||||
|
||||
function createHadlerAndGetId(): number {
|
||||
function createHandlerAndGetId(): number {
|
||||
if (!timeoutHandler) {
|
||||
timeoutHandler = new android.os.Handler(android.os.Looper.getMainLooper());
|
||||
}
|
||||
|
||||
return new Date().getUTCMilliseconds();
|
||||
timerId++;
|
||||
return timerId;
|
||||
}
|
||||
|
||||
export function setTimeout(callback: Function, milliseconds = 0): number {
|
||||
var id = createHadlerAndGetId();
|
||||
var id = createHandlerAndGetId();
|
||||
|
||||
var runnable = new java.lang.Runnable({
|
||||
run: () => {
|
||||
callback();
|
||||
|
||||
if (timeoutCallbacks && timeoutCallbacks[id]) {
|
||||
timeoutCallbacks[id] = null;
|
||||
if (timeoutCallbacks[id]) {
|
||||
delete timeoutCallbacks[id];
|
||||
}
|
||||
}
|
||||
});
|
||||
@@ -37,12 +39,12 @@ export function setTimeout(callback: Function, milliseconds = 0): number {
|
||||
export function clearTimeout(id: number): void {
|
||||
if (timeoutCallbacks[id]) {
|
||||
timeoutHandler.removeCallbacks(timeoutCallbacks[id]);
|
||||
timeoutCallbacks[id] = null;
|
||||
delete timeoutCallbacks[id];
|
||||
}
|
||||
}
|
||||
|
||||
export function setInterval(callback: Function, milliseconds = 0): number {
|
||||
var id = createHadlerAndGetId();
|
||||
var id = createHandlerAndGetId();
|
||||
var handler = timeoutHandler;
|
||||
|
||||
var runnable = new java.lang.Runnable({
|
||||
@@ -61,4 +63,4 @@ export function setInterval(callback: Function, milliseconds = 0): number {
|
||||
return id;
|
||||
}
|
||||
|
||||
export var clearInterval = clearTimeout;
|
||||
export var clearInterval = clearTimeout;
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
* iOS specific timer functions implementation.
|
||||
*/
|
||||
var timeoutCallbacks = {};
|
||||
var timerId = 0;
|
||||
|
||||
class TimerTargetImpl extends NSObject {
|
||||
static new(): TimerTargetImpl {
|
||||
@@ -25,7 +26,8 @@ class TimerTargetImpl extends NSObject {
|
||||
}
|
||||
|
||||
function createTimerAndGetId(callback: Function, milliseconds: number, shouldRepeat: boolean): number {
|
||||
var id = new Date().getUTCMilliseconds();
|
||||
timerId++;
|
||||
var id = timerId;
|
||||
|
||||
var timerTarget = TimerTargetImpl.new().initWithCallback(callback);
|
||||
var timer = NSTimer.scheduledTimerWithTimeIntervalTargetSelectorUserInfoRepeats(milliseconds / 1000, timerTarget, "tick", null, shouldRepeat);
|
||||
@@ -44,7 +46,7 @@ export function setTimeout(callback: Function, milliseconds = 0): number {
|
||||
export function clearTimeout(id: number): void {
|
||||
if (timeoutCallbacks[id]) {
|
||||
timeoutCallbacks[id].invalidate();
|
||||
timeoutCallbacks[id] = null;
|
||||
delete timeoutCallbacks[id];
|
||||
}
|
||||
}
|
||||
|
||||
@@ -52,4 +54,4 @@ export function setInterval(callback: Function, milliseconds = 0): number {
|
||||
return createTimerAndGetId(callback, milliseconds, true);
|
||||
}
|
||||
|
||||
export var clearInterval = clearTimeout;
|
||||
export var clearInterval = clearTimeout;
|
||||
|
||||
Reference in New Issue
Block a user