class person {
/** 用户类 - 姓名 */
name = ''
/** 执行函数 栈 */
funs = []
/** 定时器 数据对象 */
timeOutVal = { timeobj: {}, time: 0 }
/** 构造函数 */
constructor(name) {
/** 姓名负值 */
this.name = name
/** 加入执行函数 */
this.task({ fun: () => console.info(`Hi! ${this.name}`) })
/** 执行函数栈 */
this.next()
return this
}
/** 函数栈执行器 */
next() {
const fn = this.funs.shift()
fn && fn()
}
/** 延迟定时器 */
sleep(time) {
this.task({ fun: () => console.info(`sleep ${time}`) })
this.task({ time })
return this
}
/** 清除定时器 */
clearSleep() {
this.task({ fun: () => {
clearTimeout(this.timeOutVal.timeobj)
console.info(`clear sleep ${this.timeOutVal.time}`)
this.next()
}})
return this
}
/** 追加栈 */
task({ fun, time }) {
if (time) this.time = time
this.funs.push(() => {
const timeobj = setTimeout(() => {
fun && fun()
this.next()
}, time || 0)
if (time) this.timeOutVal = { timeobj, time }
})
}
/** 吃的的函数 */
eat(food) {
this.task({ fun: () => console.info(`${this.name} eat ${food}`) })
return this
}
/** 玩的的函数 */
paly(what) {
this.task({ fun: () => console.info(`${this.name} paly ${what}`) })
return this
}
/** 取消上一个步骤 */
cannel() {
this.funs.splice(this.funs.length - 1, 1)
return this
}
}
new person('Tomr').eat('even').paly('bad').cannel().eat('brid').paly('good').cannel().sleep(4000).cannel().eat('678687').sleep(20000).paly('444').eat('111').clearSleep().sleep(3000).eat('222')