cc-haxe/src/kernel/Timer.hx
2022-02-21 15:35:37 +01:00

38 lines
669 B
Haxe

package kernel;
using tink.CoreApi;
import cc.OS;
/**
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:Int, 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();
}
}