Timer
Einfrieren
Friert Neko genau an dem Punkt für Float Sekunden ein, wo es gerade im Programmablauf ist.
Sys.sleep(1.5); // friert für anderthalb Sekunden Neko ein
NekoTimer
mit callbacks, ähnlich haxe.Timer
haxe.Timer läuft nicht unter Neko. Es wird nicht ein constructor
new angeboten, deshalb hier eine NekoTimer-Klasse, ungetestet.NekoTimer.hx
package ; import neko.Lib; import neko.Sys; class NekoTimer { private static var threadActive:Bool = false; private static var timersList:Array<TimerInfo> = new Array<TimerInfo>(); private static var timerInterval:Float = 0.1; public static function addTimer(interval:Int, callMethod:Void->Void):Int { //setup timer thread if not yet active if (!threadActive) setupTimerThread(); //add the given timer return timersList.push(new TimerInfo(interval, callMethod, Sys.time() * 1000)) - 1; } public static function delTimer(id:Int):Void { timersList.splice(id, 1); } private static function setupTimerThread():Void { threadActive = true; neko.vm.Thread.create(function() { while (true) { Sys.sleep(timerInterval); for (timer in timersList) { if (Sys.time() * 1000 - timer.lastCallTimestamp >= timer.interval) { timer.callMethod(); timer.lastCallTimestamp = Sys.time() * 1000; } } } }); } } private class TimerInfo { public var interval:Int; public var callMethod:Void->Void; public var lastCallTimestamp:Float; public function new(interval:Int, callMethod:Void->Void, lastCallTimestamp:Float) { this.interval = interval; this.callMethod = callMethod; this.lastCallTimestamp = lastCallTimestamp; } }
Main.hx
package ; import neko.Lib; import neko.Sys; class Main { private var timerId:Int; public function new() { trace("setting up timer..."); timerId = NekoTimer.addTimer(5000, timerCallback); trace(timerId); //idle main app while (true) { } } private function timerCallback():Void { trace("it's now 5 seconds later"); NekoTimer.delTimer(timerId); trace("removed timer"); } //neko constructor static function main() { //new Main(); trace('one'); Sys.sleep(0.5); trace('and'); Sys.sleep(0.5); trace('two'); Sys.sleep(0.5); trace('end'); } }
version #10373, modified 2011-04-01 06:28:59 by TheDaySafer