Skip to content

Commit

Permalink
Create 红绿灯.js
Browse files Browse the repository at this point in the history
  • Loading branch information
Rain120 authored May 22, 2018
1 parent 8b8da47 commit a105120
Showing 1 changed file with 35 additions and 0 deletions.
35 changes: 35 additions & 0 deletions 红绿灯.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
//用js写一个简单的交通灯功能,10秒绿灯倒数,3秒黄灯倒数,5秒红灯倒数,如何让三个灯不断交替重复?
function red() {
console.log('red light');
}

function green() {
console.log('green light');
}

function yellow() {
console.log('yellow light');
}
function tick(timer, callback){
return new Promise((resolve, reject) => {
setTimeout(() => {
callback();
resolve();
}, timer);
});
}
var promise = new Promise((resolve, reject) => {
resolve()
});
function loop() {
promise.then(() => {
return tick(10000, green);
}).then(() => {
return tick(3000, yellow);
}).then(() => {
return tick(5000, red);
}).then(() => {
loop();
});
}
loop();

0 comments on commit a105120

Please sign in to comment.