setinterval
Advanced tools
Comparing version
0.2.2 / 2021-06-27 | ||
================== | ||
* feat: support invoke immediately when timer started (#2) | ||
0.2.1 / 2019-09-19 | ||
@@ -3,0 +8,0 @@ ================== |
26
index.js
@@ -23,3 +23,3 @@ 'use strict'; | ||
_task() { | ||
_runTask() { | ||
const fn = toPromise(this._fn); | ||
@@ -37,3 +37,3 @@ | ||
// check if we should contiue | ||
// check if we should continue | ||
if (this._continue) { | ||
@@ -45,14 +45,24 @@ this.setInterval(); | ||
setInterval(initialDelay) { | ||
setInterval(initialDelay = 0, invokeImmediate = false) { | ||
if (is.boolean(initialDelay)) { | ||
invokeImmediate = initialDelay; | ||
initialDelay = 0; | ||
} | ||
initialDelay = parseInt(initialDelay, 10); | ||
const setupTimer = () => { | ||
const setupTimer = invokeImmediate => { | ||
this._continue = true; | ||
this._timer = setTimeout(() => this._task(), this._period); | ||
} | ||
// invoke immediately | ||
if (invokeImmediate) { | ||
this._runTask(); | ||
} else { | ||
this._timer = setTimeout(() => this._runTask(), this._period); | ||
} | ||
}; | ||
if (!isNaN(initialDelay) && initialDelay > 0) { | ||
setTimeout(setupTimer, initialDelay); | ||
setTimeout(() => setupTimer(invokeImmediate), initialDelay); | ||
} else { | ||
setupTimer(); | ||
setupTimer(invokeImmediate); | ||
} | ||
@@ -59,0 +69,0 @@ |
{ | ||
"name": "setinterval", | ||
"version": "0.2.1", | ||
"version": "0.2.2", | ||
"keywords": [ | ||
@@ -5,0 +5,0 @@ "setInterval", |
@@ -39,14 +39,15 @@ # setinterval | ||
Timer contructor. | ||
Timer constructor. | ||
Params: | ||
- fn(*required*): function excuted after every `period`. Should be a Promise or async function or generator function or thunk. | ||
- fn(*required*): function executed after every `period`. Should be a Promise or async function or generator function or thunk. | ||
- period(*required*): timer period(*units: milliseconds*). | ||
### setInterval(initialDelay) | ||
### setInterval([initialDelay], [invokeImmediate]) | ||
Start timer after a certain delay if specified. | ||
Start timer after a certain delay(defaults to *0*) and can decide if invoke immediately(defaults to *false*). | ||
Params: | ||
- initialDelay(*optional*): Delay period(*units: milliseconds*) before timer gets triggered. *default: undefined* | ||
- initialDelay(*optional*): Delay period(*units: milliseconds*) before timer gets triggered. *default: 0* | ||
- invokeImmediate(*optional*): specify if the timer function invoke immediately.*default: false* | ||
@@ -53,0 +54,0 @@ ### clearInterval() |
@@ -22,3 +22,3 @@ 'use strict'; | ||
it('should clearInterval', done => { | ||
const timer = new Timer(function*() { | ||
const timer = new Timer(function* () { | ||
yield sleep(50); | ||
@@ -34,3 +34,3 @@ }, 100); | ||
it('should emit error event', done => { | ||
const timer = new Timer(function*() { | ||
const timer = new Timer(function* () { | ||
yield sleep(50); | ||
@@ -49,3 +49,3 @@ throw new Error('task error!'); | ||
it('should emit tick event', done => { | ||
const timer = new Timer(function*() { | ||
const timer = new Timer(function* () { | ||
yield sleep(50); | ||
@@ -88,3 +88,3 @@ }, 100); | ||
it('should support generator function', done => { | ||
const timer = new Timer(function*() { | ||
const timer = new Timer(function* () { | ||
yield sleep(50); | ||
@@ -94,3 +94,3 @@ }, 100); | ||
timer.on('tick', e => { | ||
timer.on('tick', () => { | ||
timer.clearInterval(); | ||
@@ -107,3 +107,3 @@ done(); | ||
timer.on('tick', e => { | ||
timer.on('tick', () => { | ||
timer.clearInterval(); | ||
@@ -140,2 +140,17 @@ done(); | ||
}); | ||
it('should support invoke immediately', done => { | ||
let count = 0; | ||
const timer = new Timer(async () => { | ||
++count; | ||
}, 100); | ||
timer.setInterval(true); | ||
setTimeout(() => { | ||
assert.strictEqual(count, 1); | ||
}, 50); | ||
setTimeout(() => { | ||
assert.strictEqual(count, 2); | ||
done(); | ||
}, 150); | ||
}); | ||
}); | ||
@@ -142,0 +157,0 @@ |
8476
12.13%226
11.33%81
1.25%