Function to run. If it returns false, loop exits.
Optional
interval: IntervalSpeed of loop (default: 0)
Instance to control looping.
Animation loop
const draw = () => {
// Draw on canvas
}
// Run draw() synchronised with monitor refresh rate via `window.requestAnimationFrame`
continuously(draw).start();
With delay
const fn = () => {
// Runs after one minute
}
const c = continuously(fn, { mins: 1 } );
c.start(); // Runs `fn` every minute
Control a 'continuously'
c.cancel(); // Stop the loop, cancelling any up-coming calls to `fn`
c.elapsedMs; // How many milliseconds have elapsed since start
c.ticks; // How many iterations of loop since start
c.interval; // Get/set speed of loop. Change kicks-in at next loop.
// Use .start() to reset to new interval immediately
Asynchronous callback functions are supported too:
continuously(async () => { ..});
The callback
function can receive a few arguments:
continuously( (ticks, elapsedMs) => {
// ticks: how many times loop has run
// elapsedMs: how long since last loop
}).start();
If the callback explicitly returns false, the loop will be cancelled.
continuously(ticks => {
// Stop after 100 iterations
if (ticks > 100) return false;
}).start();
You can intercept the logic for calls to start()
with onStartCalled
. It can determine
whether the start()
proceeds, if the loop is cancelled, or the whole thing disposed,
so it can't run any longer.
continuously(callback, intervalMs, {
onStartCalled:(ticks, elapsedMs) => {
// Cancel the loop after 1000ms has elapsed
if (elapsedMs > 1000) return `cancel`;
}
}).start();
To run callback
before the sleep happens, set fireBeforeWait
:
continuously(callback, intervalMs, { fireBeforeWait: true });
Timeout if you want to trigger something once.
Returns a Continuously that continually executes
callback
atinterval
rate.By default, first the sleep period happens and then the callback happens.
Call
start
to begin/reset loop. The looping stops whencancel
is called, or whencallback
returns false.