class LazyWork {
constructor (context = window) {
this._queue = []
this.context = context
window.setTimeout(_ => this._next(), 0)
}
_next () {
let fn = this._queue.shift()
fn && fn()
}
sleep (second = 0) {
this._queue.push(_ => {
window.setTimeout(_ => {
this._next()
}, second * 1000 | 0)
})
return this
}
after (sth) {
this._queue.push(_ => {
typeof sth === 'function'
? sth.call(this.context)
: console.log(sth)
this._next()
})
return this
}
before (sth) {
this._queue.unshift(_ => {
typeof sth === 'function'
? sth.call(this.context)
: console.log(sth)
this._next()
})
return this
}
}