Javascript - Time Control Library
This is a JS Librarly for manage time. Oh yes Albert, the time!
It have 1 modules by now, TimeBeats. And soon will have babySteps.
The main features are its ability for pause the timer in middle of a period, and resume the paused period and the subsecuent ticks.
###Methods
#####initialize
initialize: function ( options )
returns: this
options is an optional object with this properties:
-
ticks
number: how much periods do you want. (default is 1, and if you pass 0 it means infinity) -
period
number: is the tick's period in milliseconds. -
callback
function: Is called at the end of each period. And, if started with callBackOnStart, is called at the first period start with ( false/lastBeat/, true/startBeat/ ) arguments. With finites ticks, at the end of the last one, is called with ( true/lastBeat/, undefined/startBeat/ ) arguments.
start: function ( options )
returns: this
options is an optional object with this properties:
callBackOnStart
boolean: if true, the callback is called on start, at time 0.
stop: function ()
returns: this
pause: function ()
returns: this
Resumes the timeBeats after paused.
resume: function ( options )
returns: this
options is an optional object with this properties:
-
period
number: the new optional period in milliseconds. -
callback
function: an optional new callback overrider callback. -
setAfterResume
boolean: if true, the new period is apply after the finalization of the current truncated period, if alse is set prior to the finalization and is used for it. ( May be there is a apriori bug when you short the period, with setAfterResume in true, except if setTimeout interpret negatives numbers as zero. Or you are using CronoJs in your time machine ;). ) TODO: modified ticks number too.
returns the remaining whole ticks.
returns the current period remaining time.
returns the timer remaining time, that is the current period remaining more the remaining ticks times.
returns the state string: stopped, started or paused. Is a result of a better chrono state manage.
returns the ticks ( not the remaining else the used for start the timer )
returns this ( set the ticks, not the remaining else the used for start the timer ).
returns the period.
//for use in stopped state, I'll fix this soon.... returns this
If not in stopped state, adds remaining ticks. return this
var timeBeat = new CRNJS.TimeBeats({
ticks: 5,
period: 5000,
callback: tickFn
});
function tickFn ( lastBeat, startCall ) {
console.log( 'tickFn ( startCall, lastBeat ) = ( ' + startCall + ', ' + lastBeat + ' )');
}
timeBeat.start( { callBackOnStart: true } );
This module is for construct function sequence, you can generate a sequence of function to execute, set how many times it will be repeated ( 0 to infinity ) and intercalate waits.
On factor, you can pass the context to use as "this" in the sequenced functions, and a public interface ( described below ) will be returned.
###Factory Function
#####CRNJS.babySteps
Factory function, returns publicInterface, and sets the default context for the sequenced functions.
CRNJS.babySteps: function ( context )
returns: publicInterface
context
object: A default context for the functions in the sequence.
###publicInterface Methods
#####addNext
addNext: function ( fn, dT, context, args )
returns: publicInterface
-
fn
function: A function to run in the sequence. -
dT
number: Wait milliseconds for the fn execution, relatives to the prior fn or wait in the sequence. -
context
object: The context to apply in fn execution, if not set the once passed on factory will be used as default. -
args
Array: The array of arguments to pass to fn, if you don't pass an array, the param will be wrapped like this: [ args ].
By default the sequence is not repeated. With this method yo can set 1 to infinity repetitions.
repeat: function ( aRepetitions )
returns: publicInterface
aRepetitions
number: Set how many times will be repeated the sequence, 0 or undefined means infinity.TODO: refactor so 0 means 0 repetitions, -1, infinity.
Inserts a wait in the sequence. It is usefull after the last fn in the sequence when there is repetitions.
wait: function ( dT )
returns: publicInterface
dT
number: Wait in milliseconds.
Triggers the sequence execution.
start: function ()
returns: publicInterface
var
MyObj = {
name: 'pepe',
sayHi : function ( ) { console.log ( ' Hi! my name is ' + this.name ); },
sayBye: function ( ) { console.log ( ' Bye!' ); }
};
CRNJS.babySteps( MyObj )
.addNext( MyObj.sayHi, 0 )
.addNext( MyObj.sayBye, 500 )
.addNext(
function ( contextName ) { this.console.log ( 'no default context', contextName ); },
500,
this,
['global context'])
.wait( 500 )
.repeat( 1 )
.start();
(The MIT License)
Copyright (c) 2012 Pablo Benito <[email protected]>
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.