1.0.2 • Published 4 years ago
pausable-setinterval v1.0.2
pausable-setinterval
- setInterval,can pause and continue, can stop
- support different-interval-series
- without any dependancy
- without README 9.6k
install
- npm install pausable-setinterval
usage
- const {Task} = require('pausable-setinterval')
- var tsk = new Task(input,intervals,tasks,max_round,wait,unit='ms')
- default unit is ms
- var p = tsk.$exec()
- tsk.$pause()
- var p = tsk.$continue()
exec
const {Task} = require('pausable-setinterval')
//a customer function
var CURRENT = 0
function cufunc(x) {
x = x + 1
var curr = (new Date()).getTime();
var time = (curr-CURRENT)/1000;
(CURRENT === 0)?'':console.log("after "+time+" seconds : "+x);
CURRENT = curr
return(x)
}
var wait = 3000 //initial wait
var interval = 5000 //do cufunc every 5 seconds
var task = cufunc //task function
var tsk = new Task(666,interval,task,6,wait) //input 666, run 6 rounds
var p = tsk.$exec()
p.then(r=>{console.log(r)})
Promise { <pending> }
> after 5.008 seconds : 668
after 5.006 seconds : 669
after 5.006 seconds : 670
after 5.008 seconds : 671
after 5.008 seconds : 672
672
>
pause
var wait = 3000 //initial wait
var interval = 5000 //do cufunc every 5 seconds
var task = cufunc //task function
var tsk = new Task(666,interval,task,6,wait) //input 666, run 6 rounds
var p = tsk.$exec()
> after 5.004 seconds : 668
> after 5.007 seconds : 669
tsk.$pause()
undefined
> tsk
Task {
input: 666,
unit: 'ms',
wait: [Function: f] { unit: 'ms', duration: 0 },
tasks: [ [Function: f] { task_name: 'cufunc' } ],
intervals: [ [Function: f] { unit: 'ms', duration: 5000 } ],
max_round: 6,
cfg: { ignore_exception: true, terminator: [Function: terminator] },
round: 3,
curr_task: Promise {
<rejected> Error: paused reject
at Object.<anonymous> (/opt/JS/NV5_/pausable-setinterval/node_modules/pausable-setimeout/setimeout.js:2:33)
at Module._compile (internal/modules/cjs/loader.js:1133:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1153:10)
at Module.load (internal/modules/cjs/loader.js:977:32)
at Function.Module._load (internal/modules/cjs/loader.js:877:14)
at Module.require (internal/modules/cjs/loader.js:1019:19)
at require (internal/modules/cjs/helpers.js:77:18)
at Object.<anonymous> (/opt/JS/NV5_/pausable-setinterval/setinterval.js:10:5)
at Module._compile (internal/modules/cjs/loader.js:1133:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1153:10),
'$started_at': 1603531583904,
'$ended_at': undefined,
'$final_costed_time': undefined,
'$paused_at': 1603531584898,
'$lst_continued_at': undefined,
'$costed_times': [ 994 ],
'$state': 'paused',
'$rslt': undefined,
'$exception': undefined,
'$stop': [Function],
'$pause': [Function],
'$continue': [Function],
'$input': [ 669 ]
},
state: 'paused',
rslts: Fifo(2) [ 669, undefined, max_length: 2 ],
exceptions: Fifo(2) [
undefined,
Error: paused reject
at Object.<anonymous> (/opt/JS/NV5_/pausable-setinterval/node_modules/pausable-setimeout/setimeout.js:2:33)
at Module._compile (internal/modules/cjs/loader.js:1133:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1153:10)
at Module.load (internal/modules/cjs/loader.js:977:32)
at Function.Module._load (internal/modules/cjs/loader.js:877:14)
at Module.require (internal/modules/cjs/loader.js:1019:19)
at require (internal/modules/cjs/helpers.js:77:18)
at Object.<anonymous> (/opt/JS/NV5_/pausable-setinterval/setinterval.js:10:5)
at Module._compile (internal/modules/cjs/loader.js:1133:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1153:10),
max_length: 2
],
final: undefined
}
>
continue
//must use a variable np = .. ,because the old paused promise will be destroyed
> var np = tsk.$continue()
undefined
> after 78.276 seconds : 670
> after 5.005 seconds : 671
> after 5.006 seconds : 672
> np
Promise { 672 }
>
stop
const {Task} = require('pausable-setinterval')
//a customer function
var CURRENT = 0
function cufunc(x) {
x = x + 1
var curr = (new Date()).getTime();
var time = (curr-CURRENT)/1000;
(CURRENT === 0)?'':console.log("after "+time+" seconds : "+x);
CURRENT = curr
return(x)
}
var intervals = [1000,2000,3000,2000,1000] //intervals
//simulate several different tasks
var tasks = Array.from({length:5}).map(r=>(cufunc))
var tsk = new Task(666,intervals,tasks,3)
var p = tsk.$exec()
> after 1.003 seconds : 668
after 2.008 seconds : 669
after 3.006 seconds : 670
after 2.006 seconds : 671
after 1.005 seconds : 672
after 1.005 seconds : 673
after 2.007 seconds : 674
after 3.005 seconds : 675
after 2.004 seconds : 676
after 1.003 seconds : 677
after 1.003 seconds : 678
after 2.003 seconds : 679
after 3.006 seconds : 680
after 2.004 seconds : 681
> tsk.$stop()
undefined
> tsk
Task {
input: 666,
unit: 'ms',
wait: [Function: f] { unit: 'ms', duration: 0 },
tasks: [
[Function: f] { task_name: 'cufunc' },
[Function: f] { task_name: 'cufunc' },
[Function: f] { task_name: 'cufunc' },
[Function: f] { task_name: 'cufunc' },
[Function: f] { task_name: 'cufunc' }
],
intervals: [
[Function: f] { unit: 'ms', duration: 1000 },
[Function: f] { unit: 'ms', duration: 2000 },
[Function: f] { unit: 'ms', duration: 3000 },
[Function: f] { unit: 'ms', duration: 2000 },
[Function: f] { unit: 'ms', duration: 1000 }
],
max_round: 3,
cfg: { ignore_exception: true, terminator: [Function: terminator] },
round: 3,
curr_task: Promise {
681,
'$started_at': 1603531884881,
'$ended_at': 1603531885883,
'$final_costed_time': 1002,
'$paused_at': undefined,
'$lst_continued_at': 1603531884881,
'$costed_times': [ 1002 ],
'$state': 'resolved',
'$rslt': 681,
'$exception': undefined,
'$stop': [Function],
'$pause': [Function],
'$continue': [Function],
'$input': [ 681 ]
},
state: 'stopped',
rslts: Fifo(10) [
677,
677,
678,
678,
679,
679,
680,
680,
681,
681,
max_length: 10
],
exceptions: Fifo(10) [
undefined, undefined,
undefined, undefined,
undefined, undefined,
undefined, undefined,
undefined, undefined,
max_length: 10
],
final: 681
}
> p.then(r=>console.log(r))
Promise { <pending> }
> 681
>
set_cfg
the default is
this.cfg = {
ignore_exception:true,
terminator:function(tsk){return(tsk.round>=tsk.max_round)}
}
your can define a terminator-condition-function
const {Task} = require('pausable-setinterval')
//a customer function
var CURRENT = 0
function cufunc(x) {
x = x + 1
var curr = (new Date()).getTime();
var time = (curr-CURRENT)/1000;
(CURRENT === 0)?'':console.log("after "+time+" seconds : "+x);
CURRENT = curr
return(x)
}
var wait = 3000 //initial wait
var intervals = [1000,2000,3000,2000,1000] //intervals
//simulate several different tasks
var tasks = Array.from({length:5}).map(r=>(cufunc))
var tsk = new Task(666,intervals,tasks,3,wait)
var curr = (new Date()).getTime()
tsk.$set_cfg({
terminator: function(tsk) {
let cond = (new Date()).getTime() > (curr + 30000) //run about 30 seconds,not accurate
return(cond)
}
})
var p = tsk.$exec()
undefined
> after 1.003 seconds : 668
after 2.008 seconds : 669
after 3.005 seconds : 670
after 2.003 seconds : 671
after 1.003 seconds : 672
after 1.002 seconds : 673
after 2.004 seconds : 674
after 3.005 seconds : 675
after 2.003 seconds : 676
after 1.003 seconds : 677
after 1.004 seconds : 678
after 2.005 seconds : 679
after 3.007 seconds : 680
after 2.004 seconds : 681
> p
Promise { 681 }
>
property
{
input: 666,
unit: 'ms',
max_round: 3,
cfg: { ignore_exception: true },
round: 0,
state: 'init',
rslts: [],
exceptions: []
}
methods
tsk.set_cfg(config_dict)
var p = tsk.exec()
tsk.$pause()
var p = tsk.$continue()
tsk.stop()
LICENSE
- ISC