ton
A TON plc based timer for javascript.
The TON (On-Delay Timer) type of timer is used to control on-delay actions. When the Timer start, it wait a preset period of time to turn on is output.
Installation
$ npm install plc-ton --save
Basic Usage
const Ton = ;const Wiring = ; var myTimer = 1000 { console; } myTimer;
TON constructor([preset [, retentive [, callback [, ...args]]]])
- preset {number | string}
- retentive {boolean}
- callback {function}
- args
preset Defines the preset value for the timer in ms. Default is 1000 (1s).
retentive Set true or false the retentive behavior. When the retentive is false (default), the property Ton.V is reset when Ton.Stop is called. Counting restarts from 0. When the retentive is true, the timer retains its value when Ton.Stop is called before the Preset value is reached. Counting restarts from this value.
callback Function to call when the preset value is reached.
args arguments to pass to a callback function.
let timer1 = ;//timer whit preset value of 1,5s and retentive behaviorlet timer2 = 1500 true;//timer whit preset value of 500ms and a callback functions whit argumentlet timer3 = '500' Foo 'running';
Timer Atributes
isRetentive
Type: {boolean}
Indicate the retentive behavior of the timer. See retentive description on constructor secction.
isRunning
Type: {boolean}
Indicate the running status of the timer.
P
Type: {number}
Indicate the preset value of the timer.
Q
Type: {boolean}
Timer output. Indicate the preset value was reached.
tick
Type: {Wiring.Signal}
A signal that is emited by timer when the preset value was reached. See wiring.io for mor details about signal and slots.
V
Type: {number}
Value that increments from 0 to the preset value timer.P when the timer is running. The value can be read and tested, but not written to
Timer Methods
Start()
Start the timer. Similar to put in true the in input of a plc's ton timer. Returns a Boolean
indicating whether or not the timer was started.
let timer1 = 2000timer1 // true - the timer started ;
Stop()
Stop or pause (depents of retentive behavior) a timer. Similar to the start()
method, Stop()
returns a Boolean
indicating whether or not the timer was
stopped.
let timer1 = 2000timer1 // false - the timer is already stoppedtimer1 // true - the timer startedtimer1 // true - the timer stopped
SetAction(callback [, ...args])
Set a callback function to call when the preset value is reached. The callback function will be called whit the aruments passed to SetAcction.
{ console;}let timer1 = 2000timer1;timer1//Total Time: 2000
Development
Pull requests are welcome.
Get the code
$ git clone https://github.com/hsocarras/ton.git
Install the dependencies
$ npm install
Run the tests
$ npm test
Contributing
If you have a suggestion or found a issue, let us known and create an issue
License
This project is licensed under the MIT License - see the LICENSE.md file for details