0

clipboard.png

sinson 126
2019-03-15 提问

查看全部 4 个回答

0

已采纳
class AI{

 constructor(){

    this.queue = []; //1代表talk 2代表cancel 3代表sleep
}

talk(){
let self = this;

new Promise(function(resolve,reject){

    self.queue.push(1);
    resolve();

}).then(function(res){
    
     if(self.queue.length >= 2){
        if(self.queue[0] == 1 && queue[1] == 2){
            self.queue.splice(0,2);
        }else{
            self.queue.shift();
            console.log("talk");
        }
    }else if(self.queue[0] == 1){
        self.queue.shift();
        console.log("talk");
    }
})
return this;
}

cancel(){
var self = this;
new Promise(function(resolve,reject){
    self.queue.push(2);
    resolve();
}).then(function(res){
    if(self.queue[0] ==2){
        self.queue.shift();
        console.log("no task");
    }else{
        console.log("cancel");
    }
})
return this;
}

sleep(n){
    var self = this;
    new Promise(function(resolve,reject){
        self.queue.push(3);
        resolve();
    }).then(function(res){
        console.log(`等待${n}秒`);
        self.queue.shift();
    })
  return this;
}
}

你可能感兴趣的

推广链接