mirror of
				https://github.com/NativeScript/NativeScript.git
				synced 2025-11-04 12:58:38 +08:00 
			
		
		
		
	
		
			
				
	
	
		
			67 lines
		
	
	
		
			1.5 KiB
		
	
	
	
		
			TypeScript
		
	
	
	
	
	
			
		
		
	
	
			67 lines
		
	
	
		
			1.5 KiB
		
	
	
	
		
			TypeScript
		
	
	
	
	
	
/**
 | 
						|
  * 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 {
 | 
						|
    if (typeof (milliseconds) !== "number") {
 | 
						|
        milliseconds = 0;
 | 
						|
    }
 | 
						|
 | 
						|
    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 {
 | 
						|
    if (typeof (milliseconds) !== "number") {
 | 
						|
        milliseconds = 0;
 | 
						|
    }
 | 
						|
 | 
						|
    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;
 | 
						|
}
 |