例:obj.write("1").await(1000).write("2");// 打印 1// 等待 await 时长之后打印 2### 题目描述
2 回答
跃然一笑
TA贡献1826条经验 获得超6个赞
class Obj { constructor () { this.sleep = 0 } write (str) { if (this.sleep) { setTimeout(() => { console.log(str) }, this.sleep) } else { console.log(str) } return this } await (time) { this.sleep += time return this } } const obj = new Obj() obj.write('1').await(1000).write('2').await(3000).write(3)
互换的青春
TA贡献1797条经验 获得超6个赞
function Delay() { this.queue = Promise.resolve(); } Delay.prototype.write = function() { var args = arguments; var _this = this; this.queue = this.queue.then(function() { console.log.apply(_this, args); }); return this; }; Delay.prototype.await = function(time) { this.queue = this.queue.then(function() { return new Promise(function(resolve) { setTimeout(resolve, time); }); }); return this; };var obj = new Delay(); obj.write("1").await(1000).write("2");
添加回答
举报
0/150
提交
取消