promise实现红绿灯

<!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>Document</title>
    <style>
        .bubble{
            width: 150px;
            height: 80px;
            display: flex;
            justify-content: space-around;
            align-items: center;
            margin: 0 auto;
        }
        .light{
            width: 40px;
            height: 40px;
            border-radius: 50%;
            opacity: 0.3;
        }
        .red{
            background-color: red;
        }
        .yellow{
            background-color: yellow;
        }
        .green{
            background-color: green;
        }
        .active{
            opacity: 1;
        }
        .colbox {
            width: 150px;
            height: 200px;
            background-color: gray;
            margin: 0 auto;
        }
        .rowbox {
            width: 500px;
            height: 120px;
            background-color: gray;
        }
        .north,.south{
            width: 580px;
        }
        .middle{
            display: flex;
        }
    </style>
</head>
<body>
   <div class="container">
    <div class="north">
        <div class="bubble bubble-north">
            <div class="light red"></div>
            <div class="light yellow"></div>
            <div class="light green"></div>
        </div>
        <div class="colbox"></div>
    </div>
    <div class="middle">
        <div class="west">
            <div class="light green"></div>
            <div class="light yellow"></div>
            <div class="light red"></div>

        </div>
        <div class="rowbox"></div>
        <div class="east">
            <div class="light red"></div>
            <div class="light yellow"></div>
            <div class="light green"></div>
        </div>
    </div>
    <div class="south">
        <div class="colbox"></div>
        <div class="bubble bubble-south">
            <div class="light green"></div>
            <div id="yellow" class="light yellow"></div>
            <div class="light red"></div>
        </div>
    </div> 
   </div>
</body>
<script>
    var northLightList = document.querySelectorAll('.bubble-north div');
    var southLightList = document.querySelectorAll('.bubble-south div');
    var westLightList = document.querySelectorAll('.west div');
    var eastLightList = document.querySelectorAll('.east div');

    first();
    second();
    function first() {  
        twinkle(westLightList).then(() => {
            return twinkle(southLightList);
        }).then(() => {
            first();
        })
        
    }
    function second() {  
        twinkle(eastLightList).then(() => {
            return twinkle(northLightList);
        }).then(() => {
            second();
        })
        
    }


    function twinkle(arr) {
        return new Promise((resolve, reject) => {
            arr[0].className = 'light green active'
            setTimeout(() => {
                arr[0].className = 'light green';

                var timer = setInterval(() => {
                        if(hasClass(arr[1], 'active')){
                            arr[1].className = 'light yellow';
                        }else{
                            arr[1].className = 'light yellow active';
                        }
                    }, 200);

                setTimeout(() => {
                    clearInterval(timer);
                    arr[1].className = 'light yellow';
                    resolve()
                    arr[2].className = 'light red active';
                    
                        setTimeout(function(){
                            arr[2].className = 'light red ';
                            
                        }, 2000)
                   
                }, 1000)
            }, 2000);
        })
        
        
    }
	//判断class属性中是否含有某个属性值
    function hasClass(element, cls) {
        return (' ' + element.className + ' ').indexOf(' ' + cls + ' ') > -1;
    }
</script>
</html>
<script>
    var northLightList = document.querySelectorAll('.bubble-north div');
    var southLightList = document.querySelectorAll('.bubble-south div');
    var westLightList = document.querySelectorAll('.west div');
    var eastLightList = document.querySelectorAll('.east div');

    first();

    function first() {
        twinkle(southLightList).then(() => {
            return twinkle(westLightList)
        }).then(() => {
            first();
        })
    }
    function twinkle(arr){
        return new Promise((resolve, reject) => {
            highLight({
            info: function(){
                arr[2].className = 'light red'
                arr[0].className = 'light green active'
            },
            time: 2,
            end: function(){
                highLight({
                    info:function() {
                        arr[0].className = 'light green'
                        arr[1].className = 'light yellow active'
                    },
                    time: 1,
                    end: function() {
                        highLight({
                            info: function(){
                                arr[1].className = 'light yellow'
                                resolve();
                                arr[2].className = 'light red active'
                            },
                            time: 3,
                            end: function(){
                            
                            }
                        })
                    }
                })
            }
        
        })
        })
        
    }
        
    
    //封装一个灯
    function highLight(config){
        let interval = null;
        let time = config.time;
        config.info();
        interval = setInterval(() => {
            time--;
            if(time === 0){
                clearInterval(interval);
                config.end();
            }
        }, 1000);
    }
</script> 

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值