setinterval
Advanced tools
Comparing version
17
index.js
@@ -43,5 +43,16 @@ 'use strict'; | ||
setInterval() { | ||
this._continue = true; | ||
this._timer = setTimeout(() => this._task(), this._period); | ||
setInterval(initialDelay) { | ||
initialDelay = parseInt(initialDelay, 10); | ||
const setupTimer = () => { | ||
this._continue = true; | ||
this._timer = setTimeout(() => this._task(), this._period); | ||
} | ||
if (!isNaN(initialDelay) && initialDelay > 0) { | ||
setTimeout(setupTimer, initialDelay); | ||
} else { | ||
setupTimer(); | ||
} | ||
return this; | ||
@@ -48,0 +59,0 @@ } |
{ | ||
"name": "setinterval", | ||
"version": "0.2.0", | ||
"version": "0.2.1", | ||
"keywords": [ | ||
@@ -24,4 +24,4 @@ "setInterval", | ||
"devDependencies": { | ||
"eslint": "^3.12.2", | ||
"eslint-config-egg": "^3.2.0", | ||
"eslint": "5", | ||
"eslint-config-egg": "7", | ||
"istanbul": "^0.4.5", | ||
@@ -28,0 +28,0 @@ "mocha": "^3.2.0" |
@@ -11,4 +11,6 @@ # setinterval | ||
More reasonable setInterval for async task. | ||
We all know the drawbacks of the built-in `setInterval` in Node.js(actually js itself). | ||
It's more reasonable to start measuring period after every async task gets done. So here it is. | ||
## Install | ||
@@ -20,4 +22,4 @@ `$ npm i setinterval` | ||
const Timer = require('setinterval'); | ||
const t = new Timer(function*() { | ||
const user = yield db.User.get(id); | ||
const t = new Timer(async () => { | ||
const user = await db.User.get(id); | ||
console.log(user); | ||
@@ -29,3 +31,5 @@ }, 1000); | ||
// xxxx | ||
// after some time... | ||
// clear timer | ||
t.clearInterval(); | ||
@@ -36,13 +40,24 @@ ``` | ||
- setInterval(fn, period) | ||
### new Timer(fn, period) | ||
fn should be a Promise, a generator function or a thunk. | ||
Timer contructor. | ||
- clearInterval() | ||
Params: | ||
- fn(*required*): function excuted after every `period`. Should be a Promise or async function or generator function or thunk. | ||
- period(*required*): timer period(*units: milliseconds*). | ||
cancel timer. | ||
### setInterval(initialDelay) | ||
Start timer after a certain delay if specified. | ||
Params: | ||
- initialDelay(*optional*): Delay period(*units: milliseconds*) before timer gets triggered. *default: undefined* | ||
### clearInterval() | ||
Stop timer(can be restart again). | ||
## Events | ||
- tick | ||
### tick | ||
@@ -57,3 +72,3 @@ Triggered each time fn is finished, whenever a error is thrown. You can cancel the timer in this event. A `count` parameter is passed in the event handler which stands for how many times fn has been called. | ||
- error | ||
### error | ||
@@ -60,0 +75,0 @@ Triggered when error thrown from fn. |
@@ -114,2 +114,3 @@ 'use strict'; | ||
assert(e.message.indexOf('[Timer]: fn should be') > -1); | ||
timer.clearInterval(); | ||
done(); | ||
@@ -119,2 +120,17 @@ }); | ||
it('should support initial delay', done => { | ||
let flag = false; | ||
const timer = new Timer(async () => { | ||
flag = true; | ||
}, 100); | ||
timer.setInterval(100); | ||
timer.on('tick', () => { | ||
assert.equal(flag, true); | ||
timer.clearInterval(); | ||
done(); | ||
}); | ||
setTimeout(() => { | ||
assert.equal(flag, false); | ||
}, 150); | ||
}); | ||
}); | ||
@@ -121,0 +137,0 @@ |
7559
19.89%6
20%203
13.41%80
23.08%