From dc977b292da3c2ecba2b2be064e0054a60676e62 Mon Sep 17 00:00:00 2001 From: Felix Roos Date: Thu, 21 Mar 2024 22:38:20 +0100 Subject: [PATCH] more options for zyklus api: + allow setting custom interval functions + allow disabling phase rounding --- packages/core/zyklus.mjs | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) 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;