Comparing version 0.0.2 to 0.0.3
39
index.js
@@ -51,10 +51,8 @@ 'use strict'; | ||
Tick.prototype.setTimeout = function timeout(name, fn, ms) { | ||
var tock = this; | ||
if (tock.timers[name]) { | ||
tock.timers[name].fns.push(fn); | ||
return tock; | ||
if (this.timers[name]) { | ||
this.timers[name].fns.push(fn); | ||
return this; | ||
} | ||
tock.timers[name] = { | ||
this.timers[name] = { | ||
clear: clearTimeout, | ||
@@ -78,10 +76,8 @@ timer: setTimeout(this.tock(name, true), Tick.parse(ms)), | ||
Tick.prototype.setInterval = function interval(name, fn, ms) { | ||
var tock = this; | ||
if (tock.timers[name]) { | ||
tock.timers[name].fns.push(fn); | ||
return tock; | ||
if (this.timers[name]) { | ||
this.timers[name].fns.push(fn); | ||
return this; | ||
} | ||
tock.timers[name] = { | ||
this.timers[name] = { | ||
clear: clearInterval, | ||
@@ -138,2 +134,21 @@ timer: setInterval(this.tock(name), Tick.parse(ms)), | ||
/** | ||
* Adjust a timeout or interval to a new duration. | ||
* | ||
* @returns {Tick} | ||
* @api public | ||
*/ | ||
Tick.prototype.adjust = function adjust(name, ms) { | ||
var timer = this.timers[name] | ||
, interval; | ||
if (!timer) return this; | ||
interval = timer.clear === clearInterval; | ||
timer.clear(timer.timer); | ||
timer.timer = (interval ? setInterval : setTimeout)(this.tock(name, !interval), Tick.parse(ms)); | ||
return this; | ||
}; | ||
/** | ||
* Parse a time string and return the number value of it. | ||
@@ -140,0 +155,0 @@ * |
{ | ||
"name": "tick-tock", | ||
"version": "0.0.2", | ||
"version": "0.0.3", | ||
"description": "Timer management, never forget to clear timers again", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -30,2 +30,4 @@ # tick-tock | ||
All methods return `this` unless stated otherwise. | ||
### Tock.setTimeout() | ||
@@ -79,4 +81,19 @@ | ||
### Tock.adjust() | ||
There are cases where you sometimes need to update or change the interval of an | ||
`setTimeout` or `setInterval` for example in the case of a setTimeout which | ||
coordinates a heartbeat. In order to make this easier you call the `.adjust` | ||
method with the name of the timeout that you want to adjust and the new | ||
interval/timeout. | ||
```js | ||
tock.setTimeout('heartbeat timeout', function () {}); | ||
// you recieved a new heartbeat so you want to reset or adjust the heartbeat; | ||
tock.adjust('heartbeat timeout', '1 second'); | ||
``` | ||
## License | ||
MIT |
45
test.js
@@ -8,2 +8,6 @@ describe('ticktock', function () { | ||
function fail() { | ||
throw new Error('I should never be executed'); | ||
} | ||
beforeEach(function () { | ||
@@ -203,6 +207,2 @@ tock = new Tick(); | ||
describe('#clear', function () { | ||
function fail() { | ||
throw new Error('I should never be executed'); | ||
} | ||
it('clears multiple timeouts', function (next) { | ||
@@ -259,2 +259,39 @@ tock.setTimeout('timer', fail, '1 second'); | ||
}); | ||
describe('#adjust', function () { | ||
it('adjusts nothing for unknown timers', function () { | ||
tock.adjust('foo', '1 second'); | ||
}); | ||
it('adjusts the timeout', function (next) { | ||
tock.setTimeout('timer', fail, '10 ms'); | ||
tock.adjust('timer', '1 second'); | ||
setTimeout(function () { | ||
tock.clear(); | ||
next(); | ||
}, 500); | ||
}); | ||
it('adjusts the interval', function (next) { | ||
var ticked = false; | ||
tock.setInterval('foo', function () { | ||
var spend = Date.now() - start; | ||
if (spend < 30) { | ||
tock.adjust('foo', '100 ms'); | ||
ticked = true; | ||
} else if(spend < 150) { | ||
if (!ticked) throw new Error('I should have ticked'); | ||
tock.clear(); | ||
next(); | ||
} | ||
start = Date.now(); | ||
}); | ||
var start = Date.now(); | ||
}); | ||
}); | ||
}); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
15720
400
98