rocktimer is for implementing things such as, countdowns, stopwatches, and session expirations. Reliably start, stop, clear, reset and extend the timer. Add functions called during those events and during time intervals you define.

rocktimer uses unicode formatted time descriptors.

A demo timer/countdown application is included in the tests. To build the test document, Use npm start:

Accuracy of a scripted timer is limited by the host environment. rocktimer does not adjust itself for scenarious that reduce accuracy. When preprocessing is handled on 'start', actual start time is less accurate. When multiple functions are bound to a time interval, they are called in sequence allowing small time differences. Rocktimer is accurate enough only.

rocktimerdoes not use a popular library like jQuery. Its minified size is ~4.8kb. All methods are public and may be redefined as needed.

The forEach method and event hooks are what make rocktimer really useful.

rocktimer({

hh :0,

mm :0,

ss :1,

ms :100

}).forEach({ mm :1},function(ms){

// every 1 minute

}).forEach({ mm :2},function(ms){

// every 2 minues

}).forEach({ ss :1},function(ms){

// every 1 second

}).forEach({ hh :100},function(ms){

// every 100 hours

}).onStart(function(ms){

// timer is started

}).onStop(function(ms){

// timer is stopped

}).onExtend(function(ms){

// timer is extended

}).onClear(function(ms){

// timer is cleared

}).start();

Unlimited time intervals and event hooks may be added. Overlapping intervals are handled with rock stability. A brevity of syntax needed means less overhead for managing part of your application that using rocktimer.

rocktimer ( timeOpts )
returns a timer object. timeOpts should be an object with property-names corresponding to unicode time values.

rocktimer({

hh :1,// 1 hour

mm :0,// 0 minutes

ss :1,// 1 second

ms :100// 100 milliseconds

});

rocktimer.prototype
prototype is not a method but a property defined on the rocktimer namespace. the prototype is used by rocktimer to construct its own timer object. prototype may be accessed to redefine its default properties. Methods on the prototype include start, stop, clear, reset, extend, isActive, getremainingms.

rocktimer.prototype.start ( )

starts the timer.

rocktimer({

hh :0,

mm :0,

ss :1,

ms :100

}).onStart(function(){

console.log('timer will complete after 1100 milliseconds');

}).start();

rocktimer.prototype.stop ( )

stops the timer.

rocktimer({

hh :0,

mm :0,

ss :1,

ms :100

}).onStop(function(){

console.log('timer is stopped');

}).start().stop();

rocktimer.prototype.reset ( )

Reset values. If timer was created for 5 minutes, timer will have 5 minutes remaining after reset(), no matter what its extended time may have been. Reset also stops and clears the timer.

var rock = rocktimer({

hh :0,

mm :0,

ss :1,

ms :100

}).onStop(function(){

console.log('~2100 milliseconds have passed');

}).start();

setTimeout(function(){

rock.reset();

},1000)

rocktimer.prototype.clear ( )

Clears the timer to begins at 0. Extended times added to the timer remain on the timer.

var rock = rocktimer({

hh :0,

mm :0,

ss :1,

ms :100

}).onStop(function(){

console.log('~2100 milliseconds have passed');

}).start();

setTimeout(function(){

rock.clear();

},1000)

rocktimer.prototype.extend ( )

add more time to the timer.

rocktimer({

hh :0,

mm :0,

ss :1,

ms :100

}).onStop(function(){

console.log('~2100 milliseconds have passed');

}).start().extend({

ms :1000

});

rocktimer.prototype.isActive ( )

is the timer active?

rocktimer({

hh :0,

mm :0,

ss :1,

ms :100

}).start().isActive()// true

.stop().isActive();// false

rocktimer.prototype.forEach ( )and event hooks

The forEach method and event hooks are what make rocktimer really useful.

rocktimer({

hh :0,

mm :0,

ss :1,

ms :100

}).forEach({ mm :1},function(ms){

// every 1 minute

}).forEach({ mm :2},function(ms){

// every 2 minues

}).forEach({ ss :1},function(ms){

// every 1 second

}).forEach({ hh :100},function(ms){

// every 100 hours

}).onStart(function(ms){

// timer is started

}).onStop(function(ms){

// timer is stopped

}).onExtend(function(ms){

// timer is extended

}).onClear(function(ms){

// timer is cleared

}).start();

Unlimited time intervals and event hooks may be added. Overlapping intervals are handled with rock stability. A brevity of syntax needed means less overhead for managing part of your application that using rocktimer.

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.