function compose (fns ) {
let i = -1;
return function () {
function run(index) {
let fn = fns[index];
if (typeof fn !== "function") {
return;
}
return fn(run.bind(null,index+1));
}
return run(i+1);
}
}
class TaskController {
taskQueue = [];
constructor() {
setTimeout(() => {
compose(this.taskQueue)();
});
}
task(fn) {
this.taskQueue.push(fn);
return this;
}
// 多少毫秒以后执行下一个函数
timeout(ms) {
this.taskQueue.push(
next => setTimeout(next, ms)
);
return this;
}
timeoutFirst(ms, fn) {
this.taskQueue.push(
next => fn(), setTimeout(next, ms)
);
return this;
}
}
function builder() {
return new TaskController();
}