mirror of
https://github.com/NativeScript/NativeScript.git
synced 2025-11-05 13:26:48 +08:00
setTimeout improved and clearTimeout, setInterval, clearInterval implemented for Android
This commit is contained in:
@@ -1,10 +1,58 @@
|
|||||||
/**
|
/**
|
||||||
* Android specific global functions implementation.
|
* Android specific global functions implementation.
|
||||||
*/
|
*/
|
||||||
export function setTimeout(callback: Function, milliseconds: number): void {
|
var timeoutHandler;
|
||||||
new android.os.Handler(android.os.Looper.getMainLooper()).postDelayed(
|
var timeoutCallbacks = {};
|
||||||
new java.lang.Runnable({
|
|
||||||
run: function () { callback(); }
|
function createHadlerAndGetId() : number {
|
||||||
}),
|
if (!timeoutHandler) {
|
||||||
long(milliseconds));
|
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;
|
||||||
}
|
}
|
||||||
|
|||||||
5
globals/globals.d.ts
vendored
5
globals/globals.d.ts
vendored
@@ -1,4 +1,7 @@
|
|||||||
/**
|
/**
|
||||||
* global functions.
|
* global functions.
|
||||||
*/
|
*/
|
||||||
export declare function setTimeout(callback: Function, milliseconds: number): void;
|
export declare function setTimeout(callback: Function, milliseconds: number): number;
|
||||||
|
export declare function clearTimeout(id: number): void;
|
||||||
|
export declare function setInterval(callback: Function, milliseconds: number): number;
|
||||||
|
export declare function clearInterval(id: number): void;
|
||||||
@@ -1,5 +1,8 @@
|
|||||||
declare var module, setTimeout;
|
declare var module, setTimeout, clearTimeout, setInterval, clearInterval;
|
||||||
import globals = require("globals/globals");
|
import globals = require("globals/globals");
|
||||||
module.exports = globals;
|
module.exports = globals;
|
||||||
|
|
||||||
setTimeout = globals.setTimeout;
|
setTimeout = globals.setTimeout;
|
||||||
|
clearTimeout = globals.clearTimeout;
|
||||||
|
setInterval = globals.setInterval;
|
||||||
|
clearInterval = globals.clearTimeout;
|
||||||
Reference in New Issue
Block a user