Function timeout

Returns a Timeout that can be triggered, cancelled and reset. Use continuously for interval- based loops.

Once start() is called, callback will be scheduled to execute after interval. If start() is called again, the waiting period will be reset to interval.

const fn = () => {
console.log(`Executed`);
};
const t = timeout(fn, 60*1000);
t.start(); // After 1 minute `fn` will run, printing to the console
t.cancel();  // Cancel it from running
t.start(); // Schedule again after 1 minute
t.start(30*1000); // Cancel that, and now scheduled after 30s

// Get the current state of timeout
t.runState; // "idle", "scheduled" or "running"

Callback function receives any additional parameters passed in from start. This can be useful for passing through event data:

const t = timeout( (elapsedMs, ...args) => {
// args contains event data
}, 1000);
el.addEventListener(`click`, t.start);

Asynchronous callbacks can be used as well:

timeout(async () => {...}, 100);

If you don't expect to need to control the timeout, consider using delay, which can run a given function after a specified delay.