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