linux脚本实现红绿灯,javascript 如何实现红绿灯效果呢?
摘要:
下文講述js實(shí)現(xiàn)紅綠燈效果的方法分享,如下所示:
實(shí)現(xiàn)思路:
1.使用setinterval 重復(fù)調(diào)用顏色輸出函數(shù)
2.使用promise實(shí)現(xiàn)
例:
//使用setInterval實(shí)現(xiàn)循環(huán)調(diào)用函數(shù)
var n = 0;
function setRYG () {
if (n % 3 == 0) { console.log('red') }
else if (n % 3 == 1) { console.log('yellow') }
else { console.log('green') }
n++;
}
setInterval(function () { setRYG() },3000);
//使用Promise實(shí)現(xiàn)紅綠燈效果
async function showInfo (color, duration) {
console.log(color)
await new Promise(function (resolve) {
setTimeout(resolve, duration);
})
}
async function main () {
while (true) {
await showInfo('red', 1000)
await showInfo('yellow', 2000)
await showInfo('green', 5000)
}
}
main();
總結(jié)
以上是生活随笔為你收集整理的linux脚本实现红绿灯,javascript 如何实现红绿灯效果呢?的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: linux 网卡 巨帧,Linux Ke
- 下一篇: Java 内存 关系_JVM和Linux