From c7b96e0be8cbab778b38fe3eac8bbff1026d7981 Mon Sep 17 00:00:00 2001 From: Joshua Granick Date: Mon, 5 Nov 2018 11:20:15 -0800 Subject: [PATCH] Timer fix --- src/haxe/Timer.hx | 105 +++++++++++++++++++++++----------------------- 1 file changed, 53 insertions(+), 52 deletions(-) diff --git a/src/haxe/Timer.hx b/src/haxe/Timer.hx index df6180992..2dc0513d2 100644 --- a/src/haxe/Timer.hx +++ b/src/haxe/Timer.hx @@ -215,106 +215,107 @@ import lime.system.System; class Timer { - - + + private static var sRunningTimers:Array = []; - + private var mTime:Float; private var mFireAt:Float; private var mRunning:Bool; - - + + public function new (time:Float) { - + mTime = time; sRunningTimers.push (this); mFireAt = getMS () + mTime; mRunning = true; - + } - - + + public static function delay (f:Void -> Void, time:Int) { - + var t = new Timer (time); - + t.run = function () { - + t.stop (); f (); - + }; - + return t; - + } - - + + private static function getMS ():Float { - + return System.getTimer (); - + } - - + + public static function measure (f:Void -> T, ?pos:PosInfos):T { - + var t0 = stamp (); var r = f (); Log.trace ((stamp () - t0) + "s", pos); return r; - + } - - + + dynamic public function run () { - - - + + + } - - + + public static inline function stamp ():Float { - - return System.getTimer () / 1000; - + + var timer = System.getTimer (); + return (timer > 0 ? timer / 1000 : 0); + } - - + + public function stop ():Void { - + if (mRunning) { - + mRunning = false; - + for (i in 0...sRunningTimers.length) { - + if (sRunningTimers[i] == this) { - + sRunningTimers[i] = null; break; - + } - + } - + } - + } - - + + @:noCompletion private function __check (inTime:Float) { - + if (inTime >= mFireAt) { - + mFireAt += mTime; run (); - + } - + } - - + + }