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.
Example: Essential functionality
constfn = () => { console.log(`Executed`); }; constt = timeout(fn, 60*1000); t.start(); // After 1 minute `fn` will run, printing to the console
Example: Control execution functionality
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:
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 afterinterval
. Ifstart()
is called again, the waiting period will be reset tointerval
.Example: Essential functionality
Example: Control execution functionality
Callback function receives any additional parameters passed in from start. This can be useful for passing through event data:
Example
Asynchronous callbacks can be used as well:
If you don't expect to need to control the timeout, consider using delay, which can run a given function after a specified delay.