<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>红绿灯</title>
</head>
<body>
<script>
// 使用Promise实现红绿灯交替重复亮
function red() {
console.log('red');
}
function green() {
console.log('green');
}
function yellow() {
console.log('yellow');
}
const light = function (timer, cb) {
return new Promise(resolve => {
setTimeout(() => {
cb();
resolve();
}, timer);
});
};
const step1 = async function () {
await light(3000, red);
await light(2000, green);
await light(1000, yellow);
await step1();
};
step1();
</script>
</body>
</html>