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:
@@ -1,16 +1,6 @@
|
||||
Globals module for defining functions part of the global context - you need only to call *require* for this module and all globals will be registered. For example setTimeout:
|
||||
Globals module for defining functions part of the global context - you need only to call *require* for this module and all globals will be registered. For example:
|
||||
```js
|
||||
require("globals");
|
||||
|
||||
setTimeout(function(){ log("Test"); }, 2000);
|
||||
|
||||
var id = setTimeout(function(){ log("Test"); }, 2000);
|
||||
...
|
||||
clearTimeout(id);
|
||||
|
||||
setInterval(function(){ log("Test"); }, 2000);
|
||||
|
||||
var intervalId = setInterval(function(){ log("Test"); }, 2000);
|
||||
...
|
||||
clearInterval(intervalId)
|
||||
```
|
||||
@@ -1,58 +0,0 @@
|
||||
/**
|
||||
* Android specific global 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;
|
||||
}
|
||||
7
globals/globals.d.ts
vendored
7
globals/globals.d.ts
vendored
@@ -1,7 +0,0 @@
|
||||
/**
|
||||
* global functions.
|
||||
*/
|
||||
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,32 +0,0 @@
|
||||
/**
|
||||
* iOS specific global 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);
|
||||
}
|
||||
@@ -1,8 +1,8 @@
|
||||
declare var module, setTimeout, clearTimeout, setInterval, clearInterval;
|
||||
import globals = require("globals/globals");
|
||||
module.exports = globals;
|
||||
import timer = require("timer/timer");
|
||||
module.exports = timer;
|
||||
|
||||
setTimeout = globals.setTimeout;
|
||||
clearTimeout = globals.clearTimeout;
|
||||
setInterval = globals.setInterval;
|
||||
clearInterval = globals.clearTimeout;
|
||||
setTimeout = timer.setTimeout;
|
||||
clearTimeout = timer.clearTimeout;
|
||||
setInterval = timer.setInterval;
|
||||
clearInterval = timer.clearTimeout;
|
||||
Reference in New Issue
Block a user