实现每隔1秒,两秒,3秒输出1,2,3

tech2023-11-01  112

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) } } }

 

最新回复(0)