var myTime = function() {
this.listOne = [];
this.listTow = [];
this.listThree = [];
this.timer = null;
this.addAsynchro = function(fn, option) {
this.listOne.push({fn: fn, ...option});
return this;
}
this.addAsynchroDelay = function(fn, option) {
this.listTow.push({fn: fn, ...option});
return this;
}
this.addAsyncDelay = function(fn, option) {
this.listThree.push({fn: fn, ...option})
return this;
}
this.runAsynchro = function() {
const len = this.listOne.length;
if (len === 0) {
return;
}
let index = 0;
while(index < len) {
const fn =this.listOne[index].fn;
fn();
index ++;
}
}
this.stop = function(fn, millisecond) {
return new Promise((resolve) => {
this.timer = setTimeout(() => {
fn();
resolve();
}, millisecond)
})
}
this.runAsynchroDelay = function() {
const len = this.listTow.length;
if (len === 0) {
return;
}
let index = 0;
const doloop = (i) => {
const fn = this.listTow[i].fn;
let millisecond = 0;
let current = this.listTow[i];
if (current.offset && current.offset > 0) {
millisecond = current.offset;
current.offset = 0;
} else {
millisecond = current.millisecond;
}
this.stop(fn, millisecond)
.then(() => {
index ++;
if (index < len) {
doloop(index);
} else {
index = 0;
doloop(index);
}
})
}
doloop(0);
}
this.runAsyncDelay = function() {
const len = this.listThree.length;
if (len === 0) {
return;
}
let index = 0;
const loop = (fn, millisecond) => {
this.timer = setTimeout(() => {
fn();
loop(fn, millisecond);
}, millisecond);
}
for (let i = 0; i < len; i ++) {
loop(this.listThree[i].fn, this.listThree[i].millisecond);
}
}
this.run = function() {
this.runAsyncDelay();
this.runAsynchro();
this.runAsynchroDelay();
}
}
function a() {
console.log('a');
}
function b() {
console.log('b');
}
function c() {
console.log('c');
}
function d() {
console.log('d');
}
let times = new myTime();
times.addAsyncDelay(b, { millisecond: 3000 }).addAsyncDelay(c, { millisecond: 3000 })
times.run();
console