minsky-ticker v1.0.0
Ticker
Frame-based & native timer based timers managed by 1 class
Helps us to normalize the api between requestAnimationFrame(), setTimeout() and setInterval by providing us with 1 api and many options. Start, stop, reset and reuse timers as you want in the simplest of ways. V3 is a ES6 rewrite of V2 keeping most of its api and structure.
Class type: Manager
Dependencies
- EventDispatcher 1.0.0
Getting started
There are 2 ways to init a ticker and set up a timer. Classes may also extend from Ticker to add an extra layer of complexity.
// set up instance
let timer = new Ticker({
timeout: 100
});
// start timer
timer.start();
// stop timer
timer.stop();
// reset
timer.reset();
// Quick timer => setTimeout-like, setInterval-like
Ticker.quick(100, (e) => {
console.log('ticked');
})
Constructor Parameters
Args
Type: Object
Default: {}
Settings to set parameters at construction time
objectName
Type: String
Default: Ticker
Object name that will be used as recognisable identifier and as prefix in logs
Interface
Options
timeout
Type: Number
Default: 0
Waiting time for the instance to trigger a timout/interval vent. 0 = next frame.
isInterval
Type: Boolean
Default: false
Turns the ticker instance into an interval timer that will trigger ‘interval’ events every time the timeout is reached. Timeout events are replaced by Interval events.
autoDestroy
Type: Boolean
Default: false
Auto-destructs the instance when the timeout callbacks have run.
autoStart
Type: Boolean
Default: false
Auto-starts the instance once the constructor is done.
data
Type: Object
Default: {}
Data to keep and add to the event.data object when events are dispatched.
useTimeout
Type: Boolean
Default: false
Sets the instance to use setTimeout or setInterval instead of requestAnimationFrame.
autoReset
Type: Boolean
Default: false
Auto-resets the time property when the timeout callbacks have run. Will be ignored if isInterval = true.
Properties
timeout
Type: Number
Default: 0
Waiting time for the instance to trigger a timout/interval vent. 0 = next frame.
isInterval
Type: Boolean
Default: false
Turns the ticker instance into an interval timer that will trigger ‘interval’ events every time the timeout is reached. Timeout events are replaced by Interval events.
autoDestroy
Type: Boolean
Default: false
Auto-destructs the instance when the timeout callbacks have run.
autoStart
Type: Boolean
Default: false
Auto-starts the instance once the constructor is done.
data
Type: Object
Default: {}
Data to keep and add to the event.data object when events are dispatched.
useTimeout
Type: Boolean
Default: false
Sets the instance to use setTimeout or setInterval instead of requestAnimationFrame.
autoReset
Type: Boolean
Default: false
Auto-resets the time property when the timeout callbacks have run. Will be ignored if isInterval = true.
timerId
Type: Number
Default: 0
Timer id returned by the native timer methods.
time
Type: Number
Default: 0
Time passed since start of the timer. Resets when reset has run or at the end of ta timeout/interval.
lastTick
Type: Number
Default: 0
Last time in ms a tick has taken place. Used together with time to know if a timeout limit is reached.
running (read only)
Type: Boolean
Default: false
Flag determining if the instance is running a timer or not.
version (static read only)
Type: Boolean
Default: ''
Version number of current class definition.
Methods
start
Parameters: reset:boolean
Return: undefined
Starts the timer, resets the instance before it starts if true is passed.
stop
Parameters: reset:boolean
Return: undefined
Stops the timer, resets the instance if true is passed.
reset
Parameters: stop:boolean
Return: undefined
Resets the timer, stops the instance if true is passed.
tick
Parameters: none
Return: undefined
Forces to calculate a timeout when called. Is called internally by when a native timer ticks.
destroy
Parameters: none
Return: undefined
Destroys the instance.
quick (static)
Parameters: [timeout] [, data] [, isInterval] [, callback]
Return: Ticker
Quick way to set up a disposable ticker. It will auto run and auto destruct once its purpose is fullfilled.
To Do
- Turn timerId, time and last tick into read only parameters + make the real props ‘private’
4 years ago