let print = new Print()
print.task(1000, () => {
console.log(1)
}).task(2000, () => {
console.log(2)
}).task(3000, () => {
console.log(3)
}).start()
function Print() {
this.list = []
this.task = (time, fn) => {
this.list.push({fn, time})
return this
}
this.start = () => {
if (this.list.length > 0) {
let temp = this.list.shift()
let self = this
setTimeout(function () {
temp.fn()
self.start()
}, temp.time)
}
}
}