js 实现红绿灯变换

class LightFn{
    async run(){
        while(true){
            console.log('this is green 3000');
            await this.sleep(3000);
            console.log('this is yellow 1000');
            await this.sleep(1000);
            console.log('this is red 2000');
            await this.sleep(2000);            
        }    
    }
    sleep(duration){
        return new Promise((resolve, reject)=>{
            setTimeout(resolve ,duration);
        })
    }
}

let a = new LightFn();
a.run();

 

posted @ 2020-11-15 17:42  尖子  阅读(737)  评论(0编辑  收藏  举报