A class to delay a task and/or to control a task in a thread way, with start, stop, pause and resume.
A task cannot then run more than once at a time, to prevent concurency running.
$ npm i awake.js
/**
* @constructor
* @param {function} callback The task.
* @param {number} delay The delay before running the task.
* Sets 0 for a one shot task.
* @throws {RequiredCallbackException}
*/
function Awake ( callback, delay )
/**
* Retrieve the delay for that task.
* @method delay
* @memberof Awake
* @return {number} The delay of the task.
*/
function delay ()
/**
* Tell if the Awake instance is paused.
* @method isPaused
* @memberof Awake
* @return {boolean} True when paused, false otherwise.
*/
function isPaused ()
/**
* Tell if the Awake instance is running.
* @method isRunning
* @memberof Awake
* @return {boolean} True when running, false otherwise.
*/
function isRunning ()
/**
* Sets to pause the Awake instance.
* @method pause
* @memberof Awake
* @returns {void}
*/
function pause ()
/**
* Resume a paused Awake's instance.
* @method resume
* @memberof Awake
* @returns {void}
*/
function resume ()
/**
* Start the task, according to the delay.
* @method start
* @memberof Awake
* @returns {void}
*/
function start ()
/**
* Stop the running task.
* @method stop
* @memberof Awake
* @returns {void}
*/
function stop ()
/**
* The callback function.
* @function
* @param {function} done The function to call at the end of the loop.
* @param {number} time The started timestamp of the loop.
*/
function ( done, time )
const util = require('util');
const Awake = require('.');
let counter = 0;
const wake = new Awake( (done, time) => {
console.log( util.format("I was called at %i, %i times", time, ++counter) );
// do someting interesting...
done();
if ( counter === 10 ) wake.stop();
},500);
wake.start();