| var TickTime = 1000; | |||||
| var DT = 0; | |||||
| var LTime = 0; | |||||
| var CPUCLK = null: | |||||
| class Clock{ | class Clock{ | ||||
| constructor(){} | |||||
| constructor(){ | |||||
| this.__ticktime = 1000; | |||||
| this.__dt = 0; | |||||
| this.__ltime = 0; | |||||
| this.__out = []; | |||||
| } | |||||
| get hz(){return Math.floor(1000 / TickTime);} | |||||
| get hz(){return Math.floor(1000 / this.__ticktime);} | |||||
| set hz(hz){ | set hz(hz){ | ||||
| if(hz >= 1) | if(hz >= 1) | ||||
| TickTime = 1000 / hz; | |||||
| this.__ticktime = 1000 / hz; | |||||
| } | } | ||||
| get wired(){return (CPUCLK !== null);} | |||||
| set wire(clk){ | |||||
| if(clk == null || typeof clk === 'function') | |||||
| CPUCLK = clk; | |||||
| clk(fn){ | |||||
| if (typeof(fn) === 'function') | |||||
| this.__out.push(fn); | |||||
| } | } | ||||
| tick(){ | tick(){ | ||||
| let ctime = Date.getTime(); | |||||
| if(LTime !== 0){ | |||||
| DT += ctime - LTime; | |||||
| while(DT > TickTime){ | |||||
| if (CPUCLK) | |||||
| CPUCLK(); | |||||
| DT -= TickTime | |||||
| 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 | |||||
| } | } | ||||
| } | } | ||||
| LTime = ctime; | |||||
| this.__ltime = ctime; | |||||
| } | } | ||||
| } | } | ||||