在线观看不卡亚洲电影_亚洲妓女99综合网_91青青青亚洲娱乐在线观看_日韩无码高清综合久久

鍍金池/ 問答/HTML5  HTML/ 實現(xiàn)一個class Lazing 函數(shù),輸出如下該怎么寫?

實現(xiàn)一個class Lazing 函數(shù),輸出如下該怎么寫?

Lazing('Garry')
// 輸出 'hello Garray'

Lazing('Garry').sleep(10).eat('rice')
// 輸出 'hello Garray'
// 等待10秒...
// 輸出 'eating rice'

Lazing('Garry').eat('rice').eat('bread')
// 輸出 'eating rice'
// 輸出 'eating bread'

Lazing('Garry').sleepFirst(5).eat('rice')
// 等待5秒...
// 輸出 'hello Garray'
// 輸出 'eating rice'

難點是定時器該怎么處理?如果sleepFirst定時器是后置的怎么來實現(xiàn)?

Lazing('Garry').eat('rice').sleepFirst(5)
// 等待5秒...
// 輸出 'hello Garray'
// 輸出 'eating rice'
回答
編輯回答
瘋子范

總體思路就是 構(gòu)造一個任務(wù)隊列

class Lazing {
  constructor(item = '') {
    this.queue = [{
        key: 'init',
        val() {
          console.log('hello ' + item)
        }
      }]
  }

  eat(item) {
    this.queue.push({
      key: 'eat',
      val() {
        console.log('eating ' + item)
      }
    })
    return this
  }

  sleep(time) {
    this.queue.push({
      key: 'sleep',
      val: time * 1000
    })
    return this
  }

  sleepFirst(time) {
    this.queue.unshift({
      key: 'sleep',
      val: time * 1000
    })
    return this
  }

  exec() {
    for (let i = 0; i < this.queue.length; i++) {
      let key = this.queue[i]['key']
      let val = this.queue[i]['val']
      if (key === 'sleep') {
        this.queue.shift()
        setTimeout(this.exec.bind(this), val)
        break
      } else {
        val()
        this.queue.shift()
        i--
      }
    }
  }
}

不過調(diào)用方式稍微不一樣些,但能達到效果

new Lazing('Garry').exec()

new Lazing('Garry').sleep(3).eat('rice').exec()

new Lazing('Garry').eat('rice').eat('bread').exec()

new Lazing('Garry').sleepFirst(3).eat('rice').exec()

new Lazing('Garry').eat('rice').sleepFirst(3).exec()
2017年12月24日 17:56
編輯回答
慢半拍

粗略的demo,定時器放在后面老衲一時想不通,此demo按順序執(zhí)行

var Lazing2 = function(name){
    return {
        _name : name,
        _food : [],
        _timeLimit : 0,
        eat : function(food){
            var _this = this;
            setTimeout(function(){
                console.log(_this._name,'吃',food)
            },_this._timeLimit*1000);
            return _this
        },
        delay : function(time){
            var _this = this;
            _this._timeLimit = time;
            return _this;
        }
    }
}

Lazing2("老衲").eat('蛋糕').delay(3).eat('屎');
2018年4月8日 11:58
編輯回答
熟稔
class _Lazing{
        constructor(str){
            this.promise=new Promise(res=>this.res=res);
            this.promise=this.promise.then(()=>console.log(`hello ${str}`));
            this.t=setTimeout(()=>this.res(),0);
        }
        sleep(time){
            this.promise=this.promise.then(()=>new Promise(res=>setTimeout(res,time*1000)));
            return this;
        }
        eat(something){
            this.promise=this.promise.then(data=>console.log(`eating ${something}`));
            return this;
        }
        sleepFirst(time){
            clearTimeout(this.t);
            new Promise(res=>setTimeout(res,time*1000)).then(()=>this.res());
            return this;
        }
    }
    const Lazing=str=>new _Lazing(str);

//    Lazing('Garry')
    // 輸出 'hello Garray'

//    Lazing('Garry').sleep(10).eat('rice')
    // 輸出 'hello Garray'
    // 等待10秒...
    // 輸出 'eating rice'

    Lazing('Garry').eat('rice').eat('bread')
    // 輸出 'eating rice'
    // 輸出 'eating bread'

//    Lazing('Garry').sleepFirst(5).eat('rice')
    // 等待5秒...
    // 輸出 'hello Garray'
    // 輸出 'eating rice'

貌似構(gòu)造那部分只能異步執(zhí)行了 還沒想到其他的方法 做等高手吧

2017年12月28日 16:46