@@ -0,0 +1,37 @@ | |||
var TickTime = 1000; | |||
var DT = 0; | |||
var LTime = 0; | |||
var CPUCLK = null: | |||
class Clock{ | |||
constructor(){} | |||
get hz(){return Math.floor(1000 / TickTime);} | |||
set hz(hz){ | |||
if(hz >= 1) | |||
TickTime = 1000 / hz; | |||
} | |||
get wired(){return (CPUCLK !== null);} | |||
set wire(clk){ | |||
if(clk == null || typeof clk === 'function') | |||
CPUCLK = clk; | |||
} | |||
tick(){ | |||
let ctime = Date.getTime(); | |||
if(LTime !== 0){ | |||
DT += ctime - LTime; | |||
while(DT > TickTime){ | |||
if (CPUCLK) | |||
CPUCLK(); | |||
DT -= TickTime | |||
} | |||
} | |||
LTime = ctime; | |||
} | |||
} | |||
module.exports = Clock; |