cc-haxe/src/kernel/Timer.hx

39 lines
771 B
Haxe
Raw Normal View History

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