LazyMan 自己的笨办法
看了LazyMan,先用自己的笨办法写了个,只是为了实现功能,其他要求都没达到
本人是菜鸟,不喜勿喷
function LazyMan (e) {
console.log(e)
var time = 0, fstime = 0
return {
eat (e) {
setTimeout(() => {
setTimeout(() => {
console.log('eat ' + e)
}, (fstime + time) * 1000)
}, 0)
return this
},
sleepFirst (e) {
fstime = e
setTimeout(() => {
console.log('sleepFirst :'+ (+new Date - a))
}, (fstime + time) * 1000)
return this
},
sleep (e) {
setTimeout(() => {
time += e
setTimeout(() => {
console.log('sleep :'+ (+new Date - a))
}, (fstime + time) * 1000)
}, time)
return this
},
}
}
a = +new Date
LazyMan("Hank").sleep(2).eat("dinner")
LazyMan("Hank").eat("dinner").eat("supper")
LazyMan("Hank").sleepFirst(2).eat("supper")
LazyMan("Hank").sleep(2).sleepFirst(3).eat("supper")
LazyMan('Tony').eat('lunch').sleep(1).eat('dinner').sleepFirst(2).sleep(3).eat('junk food')
网友评论