https://github.com/guitarneck/awake.js
A class to delay a task and/or to control a task in a thread way, with start, stop, pause and resume.
https://github.com/guitarneck/awake.js
Last synced: 4 months ago
JSON representation
A class to delay a task and/or to control a task in a thread way, with start, stop, pause and resume.
- Host: GitHub
- URL: https://github.com/guitarneck/awake.js
- Owner: guitarneck
- License: mit
- Created: 2020-04-17T11:56:17.000Z (about 5 years ago)
- Default Branch: master
- Last Pushed: 2022-11-01T19:52:30.000Z (over 2 years ago)
- Last Synced: 2025-02-07T21:05:46.906Z (4 months ago)
- Language: JavaScript
- Size: 45.9 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Awake.js
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.
# Install
```bash
$ npm i awake.js
```# Class Awake
## constructor
### ``new Awake(function, [integer])``
```javascript
/**
* @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 )
```## Methods
### ``delay()``
```javascript
/**
* Retrieve the delay for that task.
* @method delay
* @memberof Awake
* @return {number} The delay of the task.
*/
function delay ()
```### ``isPaused()``
```javascript
/**
* Tell if the Awake instance is paused.
* @method isPaused
* @memberof Awake
* @return {boolean} True when paused, false otherwise.
*/
function isPaused ()
```### ``isRunning()``
```javascript
/**
* Tell if the Awake instance is running.
* @method isRunning
* @memberof Awake
* @return {boolean} True when running, false otherwise.
*/
function isRunning ()
```### ``pause()``
```javascript
/**
* Sets to pause the Awake instance.
* @method pause
* @memberof Awake
* @returns {void}
*/
function pause ()
```### ``resume()``
```javascript
/**
* Resume a paused Awake's instance.
* @method resume
* @memberof Awake
* @returns {void}
*/
function resume ()
```### ``start()``
```javascript
/**
* Start the task, according to the delay.
* @method start
* @memberof Awake
* @returns {void}
*/
function start ()
```### ``stop()``
```javascript
/**
* Stop the running task.
* @method stop
* @memberof Awake
* @returns {void}
*/
function stop ()
```
## callback```javascript
/**
* 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 )
```# Usage
```javascript
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();
```# License
[MIT © guitarneck](./LICENSE)