class Clock{ constructor(){ this.__ticktime = 1000; this.__dt = 0; this.__ltime = 0; this.__out = []; } get hz(){return Math.floor(1000 / this.__ticktime);} set hz(hz){ if(hz >= 1) this.__ticktime = 1000 / hz; } clk(fn){ if (typeof(fn) === 'function') this.__out.push(fn); } tick(){ let ctime = (new Date()).getTime(); if(this.__ltime !== 0){ this.__dt += ctime - this.__ltime; while(this.__dt > this.__ticktime){ this.__out.forEach((fn)=>{fn();}); this.__dt -= this.__ticktime } } this.__ltime = ctime; } } module.exports = Clock;