clockmaker
Advanced tools
Comparing version 1.1.2 to 1.2.0
{ | ||
"name": "clockmaker", | ||
"main": "clockmaker.js", | ||
"version": "1.1.2", | ||
"version": "1.2.0", | ||
"authors": [ | ||
@@ -6,0 +6,0 @@ "Ramesh Nair <ram@hiddentao.com>" |
@@ -111,5 +111,5 @@ (function (root, factory) { | ||
if (this._async) { | ||
this._fn.call(this._fnThis, this._doAfterTick); | ||
this._fn.call(this._fnThis, this, this._doAfterTick); | ||
} else { | ||
this._fn.call(this._fnThis); | ||
this._fn.call(this._fnThis, this); | ||
this._doAfterTick(); | ||
@@ -139,2 +139,3 @@ } | ||
* Start the timer. | ||
* @return {this} | ||
*/ | ||
@@ -158,2 +159,4 @@ Timer.prototype.start = function() { | ||
* This resets the internal timer to start from now. | ||
* | ||
* @return {this} | ||
*/ | ||
@@ -166,2 +169,4 @@ Timer.prototype.synchronize = function() { | ||
this._scheduleNextTick(); | ||
return this; | ||
}; | ||
@@ -174,2 +179,3 @@ | ||
* Stop the timer. | ||
* @return {this} | ||
*/ | ||
@@ -191,2 +197,3 @@ Timer.prototype.stop = function() { | ||
* Set the timer delay. | ||
* @return {this} | ||
*/ | ||
@@ -202,2 +209,12 @@ Timer.prototype.setDelay = function(delayMs) { | ||
/** | ||
* Get the timer delay. | ||
* @return {Integer} The current timer delay. | ||
*/ | ||
Timer.prototype.getDelay = function(delayMs) { | ||
return this._delayMs; | ||
}; | ||
/** | ||
* Get whether this timer has stopped. | ||
@@ -204,0 +221,0 @@ */ |
{ | ||
"name": "clockmaker", | ||
"version": "1.1.2", | ||
"version": "1.2.0", | ||
"description": "Flexible Javascript timers which can be paused and modified on-the-fly", | ||
@@ -5,0 +5,0 @@ "main": "clockmaker.js", |
@@ -5,3 +5,3 @@ # Clockmaker | ||
A flexible timer management system for Javascript. | ||
Flexible timers for Javascript which can be paused and modified on-the-fly. | ||
@@ -15,3 +15,3 @@ Clockmaker is inspired by [Mozilla's MiniDaemon](https://developer.mozilla.org/en-US/docs/Web/API/window.setInterval#A_little_framework) and provides an alternative to the built-in `setTimeout` and `setInterval` functions. It is | ||
* Stop and restart timers. | ||
* Change the timer delay in real-time. | ||
* Change the timer interval in real-time. | ||
* Start and stop multiple timers in one go. | ||
@@ -47,3 +47,3 @@ * Robust error handling. | ||
These examples are all running in node.js. At the top of each example assume | ||
we have the following: | ||
you have the following: | ||
@@ -62,10 +62,11 @@ ```javascript | ||
```javascript | ||
Timer(function() { | ||
console.log('2 seconds done'); | ||
Timer(function(timer) { | ||
console.log(timer.getDelay() + ' millseconds done'); | ||
}, 2000).start(); | ||
``` | ||
Notice how `start()` needs to be called to kick-off the timer. You can also | ||
explicitly construct the `Timer` object: | ||
Notice how `start()` needs to be called to kick-off the timer. Also notice how the `Timer` instance is passes as an argument to the handler, allowing us to query and control the timer from within our handler. | ||
You can also explicitly construct the `Timer` object: | ||
```javascript | ||
@@ -89,20 +90,34 @@ var timer = new Timer(function() { | ||
// ... some time later | ||
// ... >2 seconds later | ||
timer.start(); | ||
timer.start(); // has no effect | ||
timer.isStopped(); // true | ||
``` | ||
And you can stop a timer from executing its handler in the first place: | ||
```javascript | ||
var timer = new Timer(function() { | ||
console.log('You should never see this line'); | ||
}, 2000); | ||
timer.start(); | ||
timer.isStopped(); // false | ||
// ... <2 seconds later | ||
timer.stop(); | ||
timer.isStopped(); // true | ||
``` | ||
### setInterval | ||
We simulate `setInterval` behaviour by setting `repeat: true` in the options. | ||
You can simulate `setInterval` behaviour by setting `repeat: true` in the options. | ||
```javascript | ||
var timer = new Timer(function() { | ||
console.log('Another 2 seconds done'); | ||
Timer(function() { | ||
console.log('Another 2 seconds passed'); | ||
}, 2000, { | ||
repeat: true | ||
}); | ||
timer.start(); | ||
}).start(); | ||
``` | ||
@@ -115,4 +130,4 @@ | ||
var timer = new Timer(function() { | ||
console.log('Another 2 seconds done'); | ||
Timer(function(timer) { | ||
console.log('Another 2 seconds passed'); | ||
@@ -125,22 +140,15 @@ count++; | ||
repeat: true | ||
}); | ||
timer.start(); | ||
}).start(); | ||
``` | ||
We can change the delay interval in real-time: | ||
You can change the delay interval in real-time: | ||
```javascript | ||
var delayMs = 1000; | ||
var timer = new Timer(function() { | ||
Timer(function(timer) { | ||
console.log('Next tick will take 1 second longer'); | ||
delayMs += 1000; | ||
timer.setDelay(delayMs); | ||
timer.setDelay(timer.getDelay() + 1000); | ||
}, delayMs, { | ||
repeat: true | ||
}); | ||
timer.start(); | ||
}).start(); | ||
``` | ||
@@ -179,7 +187,7 @@ | ||
The timer waits for the handler to finish executing before scheduling the next | ||
tick. But what if our handler is asynchronous? we have to inform the timer of | ||
this: | ||
tick. But what if the handler is asynchronous? you can inform the timer of | ||
this and be given a callback: | ||
```javascript | ||
var timer = new Timer(function(cb) { | ||
var timer = new Timer(function(timer, cb) { | ||
// ... do some stuff | ||
@@ -195,7 +203,6 @@ cb(); | ||
Until our handler invokes the `cb()` callback (see above) the timer will not | ||
schedule the next tick. This allows us to decide whether we want to schedule | ||
the next tick straight away or once we've done all our necessary work inside | ||
our handler. | ||
In this case, until your handler invokes the `cb()` callback (see above) the timer will not schedule the next tick. This allows you to decide whether you want to schedule | ||
the next tick straight away (i.e. calling `cb()` straight away) or once all necessary work inside the handler is done (i.e. calling `cb()` at the end). | ||
You can also use the callback to [handle errors](#handling-errors). | ||
@@ -221,3 +228,3 @@ ### This context | ||
Sometimes we may want to reset a timer that's already running, i.e. stop and | ||
Sometimes you may want to reset a timer that's already running, i.e. stop and | ||
then restart it without actually having to do so: | ||
@@ -243,3 +250,3 @@ | ||
We can pass in an `onError` handler to be informed of errors: | ||
You can pass in an `onError` handler to be informed of errors: | ||
@@ -259,3 +266,3 @@ ```javascript | ||
```javascript | ||
new Timer(function(cb) { | ||
new Timer(function(timer, cb) { | ||
cb(new Error('A dummy error')); | ||
@@ -270,5 +277,7 @@ }, 2000, { | ||
_Note: If you don't pass in an `onError` handler then errors are ignored._ | ||
### Multiple timers | ||
We can control multiple timers at a time by using the `Timers` interface. | ||
You can control multiple timers at a time by using the `Timers` interface. | ||
@@ -282,3 +291,3 @@ ```javascript | ||
timer1.start(); // we can start them one a a time, or... | ||
timer1.start(); // you can start them one a a time, or... | ||
@@ -326,7 +335,7 @@ timers.start(); // ...start them all at once | ||
To build the code: | ||
To build the code and run the tests: | ||
$ npm install -g gulp | ||
$ npm install | ||
$ gulp build <-- this will build the code and run the tests | ||
$ gulp build | ||
@@ -336,3 +345,3 @@ | ||
Contributions are welcome! Please see CONTRIBUTING.md. | ||
Contributions are welcome! Please see [CONTRIBUTING.md](https://github.com/hiddentao/clockmaker/blob/master/CONTRIBUTING.md). | ||
@@ -342,2 +351,2 @@ | ||
MIT - see LICENSE.md | ||
MIT - see [LICENSE.md](https://github.com/hiddentao/clockmaker/blob/master/LICENSE.md) |
16238
231
335