cc-haxe/src/kernel/Timer.hx

37 lines
730 B
Haxe
Raw Normal View History

2021-12-20 00:55:30 +00:00
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<Array<Dynamic>>;
/**
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);
}
}