mirror of
https://github.com/NativeScript/NativeScript.git
synced 2025-11-05 13:26:48 +08:00
timer moved in separate module
This commit is contained in:
58
timer/timer.android.ts
Normal file
58
timer/timer.android.ts
Normal file
@@ -0,0 +1,58 @@
|
||||
/**
|
||||
* Android specific timer functions implementation.
|
||||
*/
|
||||
var timeoutHandler;
|
||||
var timeoutCallbacks = {};
|
||||
|
||||
function createHadlerAndGetId() : number {
|
||||
if (!timeoutHandler) {
|
||||
timeoutHandler = new android.os.Handler(android.os.Looper.getMainLooper());
|
||||
}
|
||||
|
||||
return new Date().getUTCMilliseconds();
|
||||
}
|
||||
|
||||
export function setTimeout(callback: Function, milliseconds: number): number {
|
||||
var id = createHadlerAndGetId();
|
||||
|
||||
var runnable = new java.lang.Runnable({
|
||||
run: function () {
|
||||
callback();
|
||||
timeoutCallbacks[id] = null;
|
||||
}
|
||||
});
|
||||
|
||||
if (!timeoutCallbacks[id]) {
|
||||
timeoutCallbacks[id] = runnable;
|
||||
}
|
||||
|
||||
timeoutHandler.postDelayed(runnable, long(milliseconds));
|
||||
|
||||
return id;
|
||||
}
|
||||
|
||||
export function clearTimeout(id: number): void {
|
||||
if (timeoutCallbacks[id]) {
|
||||
timeoutHandler.removeCallbacks(timeoutCallbacks[id]);
|
||||
timeoutCallbacks[id] = null;
|
||||
}
|
||||
}
|
||||
|
||||
export function setInterval(callback: Function, milliseconds: number): number {
|
||||
var id = createHadlerAndGetId();
|
||||
|
||||
var runnable = new java.lang.Runnable({
|
||||
run: function () {
|
||||
callback();
|
||||
timeoutHandler.postDelayed(runnable, long(milliseconds));
|
||||
}
|
||||
});
|
||||
|
||||
if (!timeoutCallbacks[id]) {
|
||||
timeoutCallbacks[id] = runnable;
|
||||
}
|
||||
|
||||
timeoutHandler.postDelayed(runnable, long(milliseconds));
|
||||
|
||||
return id;
|
||||
}
|
||||
Reference in New Issue
Block a user