cc-haxe/src/kernel/Timer.hx
2022-12-19 21:06:23 +01:00

38 lines
671 B
Haxe

package kernel;
import cc.OS;
using tink.CoreApi;
/**
Wrapper class for using timer.
**/
class Timer {
private final timerID:Int;
private final callback:Callback<Noise>;
private final timerLink:CallbackLink;
/**
Create new timer with timeout in seconds.
**/
public function new(timeout:Float, callback:Callback<Noise>) {
timerID = OS.startTimer(timeout);
this.callback = callback;
timerLink = KernelEvents.instance.onTimer.handle(timerID -> {
if (this.timerID == timerID) {
callback.invoke(null);
timerLink.cancel();
}
});
}
/**
Cancle timer.
**/
public function cancle() {
OS.cancelTimer(timerID);
timerLink.cancel();
}
}