1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
export default class Schedule{
constructor(){
this._delay=0
this.p = null
}
timer(task,ms){
return new Promise((resolve,reject)=>{
setTimeout(()=>{
task && task()
resolve()
},ms)
})
}
task(task){
const {_delay:delay,timer,p}=this
this.p = p ?p.then(()=>timer(task,delay)) :timer(task,delay)
return this
}
delay(_delay){
this._delay = _delay
return this
}
}
// 用法
new Schedule().task(task1)
.delay(1000).task(task2)
.delay(500).task(task3)