<style>
#light{
/* background:red; */
width:50px;
height:50px;
border-radius:50%;
border:1px solid #000;
}
</style>
<body>
<div id="light"></div>
<script>
function sleep(duration) {
return new Promise(function(resolve, reject) {
setTimeout(resolve,duration);
})
}
async function changeLightColor(duration, color){
document.getElementById('light').style.background = color
console.log('color', color)
await sleep(duration)
}
async function act() {
while (true) {
await changeLightColor(30000, 'green');
await changeLightColor(3000, 'yellow');
await changeLightColor(20000, 'red');
}
}
act()
</script>
</body>
请通过 Promise、async、await三者实现简单的信号灯转换流程:绿灯 30s、黄灯 3s、红灯 20s,往复循环
于 2022-05-17 00:29:49 首次发布