A JavaScript tool to imitate setTimeout
functionality to extend control over it.
npm install timeoutcontrol
TimeoutControl
accepts any parameters that the native setTimeout
will accept.
const timeout = new TimeoutControl(callback[, delay[, param1, param2, ...]]);
Returns the id of the timeout.
Returns an array of all params (i.e., param1
, param2
, ...).
Returns the duration of the timeout.
Returns the time the timeout starts.
Returns the time the timeout is paused/stopped.
Returns the callback function to run when the timeout ends.
Returns the time left before the timeout ends.
Returns a boolean of whether the timeout has ended.
Pause the ongoing timeout.
Resume the paused timeout.
Clear the timeout, then start the timeout with the arguments previously passed to the instance.
Cancel the timeout.
const duration = 5000;
const callback = function(){
console.log(...arguments);
}
const timeout = new TimeoutControl(callback, duration, 1, 2, 3, 4, 5);
/* ...SOME OPERATIONS / CONDITIONS */
timeout.pause();
/* ...SOME OPERATIONS / CONDITIONS */
timeout.resume();
/* ...SOME OPERATIONS / CONDITIONS */
timeout.clear();
/* ...SOME OPERATIONS / CONDITIONS */
timeout.restart();