1227
1227 今日计划
每天工作前/前一天晚上 做好计划:重点工作、自我提升、其他,提交地址
重点工作
- 房源列表页 + 所有接口
- 图片上传
自我提升
- mini-vue
- 链表算法
其他
今日总结
问题,原因,解决方式,优化,巧妙实现,新知识
手撕代码/算法
防抖节流等各种手写,http和网络,浏览器原理,性能优化,Webpack
实现 LazyMan
js
LazyMan("Hank")//输出:
// Hi! This is Hank !
LazyMan("Hank").sleep(3).eat("dinner")//输出:
// Hi! This is Hank !// 等待3秒
// wake up after 3 Eat dinner~
LazyMan("Hank").eat("lunch").eat("dinner")//输出:
// Hi! This is Hank!
// Eat lunch~
// Eat dinner~
LazyMan("Hank").sleepFirst(2).eat("lunch").sleep(3).eat("dinner")// 输出:
// 等待2秒
// wake up after 2
// Hi! This is Hank
// Eat lunch~
// 等待3秒
// wake up after 3l / Eat dinner~
// 以此类推
实现:
js
class _LazyMan {
constructor (name) {
this.taskQueue = [];
this.runTimer = null;
this.sayHi(name);
}
run () {
if (this.runTimer) {
// 多个任务执行的时候,会清除 执行操作,重新执行,此时获取了新的任务队列
clearTimeout(this.runTimer);
}
console.log(this.taskQueue);
this.runTimer = setTimeout(async () => {
for (let asyncFun of this.taskQueue) {
await asyncFun()
}
this.taskQueue.length = 0;
this.runTimer = null;
})
return this;
}
sayHi (name) {
this.taskQueue.push(async () => console.log(`Hi, this is ${name}`));
return this.run();
}
eat (food) {
this.taskQueue.push(async () => console.log(`Eat ${food}`));
return this.run();
}
sleep (second) {
this.taskQueue.push(async () => {
console.log(`Sleep ${second} s`)
return this._timeout(second)
});
return this.run();
}
sleepFirst (second) {
this.taskQueue.unshift(async () => {
console.log(`Sleep first ${second} s`)
return this._timeout(second);
});
return this.run();
}
async _timeout (second) {
await new Promise(resolve => {
setTimeout(resolve, second * 1e3);
})
}
}
// 测试
var lazyMan = name => new _LazyMan(name)
lazyMan('Hank');
// lazyMan('Hank').sleep(3).eat('dinner');
// lazyMan('Hank').eat('dinner').eat('supper');
lazyMan('Hank').sleepFirst(5).eat('supper').sleep(3).eat('apple');
相关资料:
好文推荐/面经相关/博客项目推荐
有感好文,面经,博客,项目等