diff --git a/packages/core/zyklus.mjs b/packages/core/zyklus.mjs index d3c48667..0fb4ccc4 100644 --- a/packages/core/zyklus.mjs +++ b/packages/core/zyklus.mjs @@ -7,6 +7,9 @@ function createClock( duration = 0.05, // duration of each cycle interval = 0.1, // interval between callbacks overlap = 0.1, // overlap between callbacks + setInterval = globalThis.setInterval, + clearInterval = globalThis.clearInterval, + round = true, ) { let tick = 0; // counts callbacks let phase = 0; // next callback time @@ -22,7 +25,7 @@ function createClock( } // callback as long as we're inside the lookahead while (phase < lookahead) { - phase = Math.round(phase * precision) / precision; + phase = round ? Math.round(phase * precision) / precision : phase; phase >= t && callback(phase, duration, tick, t); phase < t && console.log('TOO LATE', phase); // what if latency is added from outside? phase += duration; // increment phase by duration @@ -35,7 +38,10 @@ function createClock( onTick(); intervalID = setInterval(onTick, interval * 1000); }; - const clear = () => intervalID !== undefined && clearInterval(intervalID); + const clear = () => { + intervalID !== undefined && clearInterval(intervalID); + intervalID = undefined; + }; const pause = () => clear(); const stop = () => { tick = 0;