package kernel; import util.EventBus.EventBusListner; import cc.OS; /** Wrapper class for using timer. **/ class Timer { private final timerID:Int; private final callback:Void->Void; private final timerListner:EventBusListner>; /** Create new timer with timeout in seconds. **/ public function new(timeout: Int, cb: Void->Void) { timerID = OS.startTimer(timeout); callback = cb; timerListner = KernelEvents.instance.on("timer",(params)->{ if (params[1] == timerID){ cb(); KernelEvents.instance.removeListner(timerListner); } }); } /** Cancle timer. **/ public function cancle() { OS.cancelTimer(timerID); KernelEvents.instance.removeListner(timerListner); } }