Update Timer

This commit is contained in:
Joshua Granick
2016-12-07 10:08:03 -08:00
parent 0d080ca49f
commit 0ccba53c86

View File

@@ -41,19 +41,20 @@ package haxe;
the child class. the child class.
**/ **/
class Timer { class Timer {
#if (flash || js || java || python)
#if (flash || js) #if (flash || js)
private var id : Null<Int>; private var id : Null<Int>;
#elseif java #elseif java
private var timer : java.util.Timer; private var timer : java.util.Timer;
private var task : java.util.TimerTask; private var task : java.util.TimerTask;
#else
private var event : MainLoop.MainEvent;
#end #end
/** /**
Creates a new timer that will run every `time_ms` milliseconds. Creates a new timer that will run every `time_ms` milliseconds.
After creating the Timer instance, it calls `this].run` repeatedly, After creating the Timer instance, it calls `this.run` repeatedly,
with delays of `time_ms` milliseconds, until `this.stop` is called. with delays of `time_ms` milliseconds, until `this.stop` is called.
The first invocation occurs after `time_ms` milliseconds, not The first invocation occurs after `time_ms` milliseconds, not
@@ -71,6 +72,13 @@ class Timer {
#elseif java #elseif java
timer = new java.util.Timer(); timer = new java.util.Timer();
timer.scheduleAtFixedRate(task = new TimerTask(this), haxe.Int64.ofInt(time_ms), haxe.Int64.ofInt(time_ms)); timer.scheduleAtFixedRate(task = new TimerTask(this), haxe.Int64.ofInt(time_ms), haxe.Int64.ofInt(time_ms));
#else
var dt = time_ms / 1000;
event = MainLoop.add(function() {
@:privateAccess event.nextRun += dt;
run();
});
event.delay(dt);
#end #end
} }
@@ -93,9 +101,16 @@ class Timer {
#end #end
id = null; id = null;
#elseif java #elseif java
timer.cancel(); if(timer != null) {
timer = null; timer.cancel();
timer = null;
}
task = null; task = null;
#else
if( event != null ) {
event.stop();
event = null;
}
#end #end
} }
@@ -132,8 +147,6 @@ class Timer {
return t; return t;
} }
#end
/** /**
Measures the time it takes to execute `f`, in seconds with fractions. Measures the time it takes to execute `f`, in seconds with fractions.
@@ -158,7 +171,7 @@ class Timer {
The value itself might differ depending on platforms, only differences The value itself might differ depending on platforms, only differences
between two values make sense. between two values make sense.
**/ **/
public static function stamp() : Float { public static inline function stamp() : Float {
#if flash #if flash
return flash.Lib.getTimer() / 1000; return flash.Lib.getTimer() / 1000;
#elseif (neko || php) #elseif (neko || php)
@@ -167,8 +180,11 @@ class Timer {
return Date.now().getTime() / 1000; return Date.now().getTime() / 1000;
#elseif cpp #elseif cpp
return untyped __global__.__time_stamp(); return untyped __global__.__time_stamp();
#elseif python
return Sys.cpuTime();
#elseif sys #elseif sys
return Sys.time(); return Sys.time();
#else #else
return 0; return 0;
#end #end
@@ -258,7 +274,7 @@ class Timer {
} }
public static function stamp ():Float { public static inline function stamp ():Float {
return System.getTimer () / 1000; return System.getTimer () / 1000;