Returns a Continuously that continually executes callback at interval rate.
By default, first the sleep period happens and then the callback happens.
Call start to begin/reset loop. The looping stops when cancel is called, or when callback returns false.
Example
Animation loop
constdraw = () => { // Draw on canvas }
// Run draw() synchronised with monitor refresh rate via `window.requestAnimationFrame` continuously(draw).start();
Example
With delay
constfn = () => { // Runs after one minute } constc = continuously(fn, { mins:1 } ); c.start(); // Runs `fn` every minute
Example
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) returnfalse; }).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 callbackbefore the sleep happens, set fireBeforeWait:
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.Example
Animation loop
Example
With delay
Example
Control a 'continuously'
Asynchronous callback functions are supported too:
The
callback
function can receive a few arguments:If the callback explicitly returns false, the loop will be cancelled.
You can intercept the logic for calls to
start()
withonStartCalled
. It can determine whether thestart()
proceeds, if the loop is cancelled, or the whole thing disposed, so it can't run any longer.To run
callback
before the sleep happens, setfireBeforeWait
:See
Timeout if you want to trigger something once.