sodiumjs
Version:
A Functional Reactive Programming (FRP) library for JavaScript
31 lines (27 loc) • 838 B
text/typescript
import { TimerSystem, TimerSystemImpl } from "./TimerSystem";
/**
* A timer system implementation using seconds as the time unit.
*/
export class SecondsTimerSystem extends TimerSystem {
constructor() {
super(new SecondsTimerSystemImpl());
}
}
class SecondsTimerSystemImpl extends TimerSystemImpl {
/**
* Set a timer that will execute the specified callback at the specified time.
* @return A function that can be used to cancel the timer.
*/
setTimer(t : number, callback : () => void) : () => void
{
let timeout = setTimeout(callback, Math.max((t - this.now()) * 1000, 0));
return () => { clearTimeout(timeout); }
}
/**
* Return the current clock time.
*/
now() : number
{
return Date.now() * 0.001;
}
}