javascript定时器setInterval一个案例----黄绿灯

题目:写一个路口红绿灯,现在正对前方是红灯,从20秒开始倒计时,当数值到0时转成绿灯,绿色灯限时20秒,当数值到0时转成黄灯并重新开始倒数3秒,经过3秒的等待,转成红灯20秒,循环往复以上过程

代码:1.1

<body>
    <!-- 写一个路口红绿灯,现在正对前方是红灯,20秒开始倒计时,当数值到0时转成绿灯,绿色灯限时20,
    当数值到0时转成黄灯并重新开始倒数3,经过3秒的等待,转成红灯20,循环往复以上过程 -->
    <button>关闭定时器</button>
    <script>
        // 1.1
        var time = 43;
        var time1 = 20;
        var time2 = 3;
        var time3 = 20;
        var timer = setInterval(function() {
            time--;
            if (time >= 23) {
                console.log(('绿灯' + time1 + '秒'));
                time1--;
                if (time1 <= 0) {
                    time1 = 20
                }

            } else if (time >= 20) {
                console.log(('黄灯' + time2 + '秒'));
                time2--;
                if (time2 <= 0) {
                    time2 = 3
                }

            } else if (time >= 0) {
                console.log(('红灯' + time3 + '秒'));
                time3--;
                if (time3 <= 0) {
                    time3 = 20
                }

            } else {
                time = 43;
            }
        }, 100)
        var btn = document.querySelector('button');
        btn.onclick = function() {
            clearInterval(timer)
        }
        </script>

1.2

<body>
<button>关闭定时器</button>
    <script>
       var  stop  =  setInterval(a,  100);        
        var  count  =  30;        
        function  a()  {            
            count-- ;           
            console.log(count);            
            if  (count  ==  0)  {                
                console.log('绿灯到了 ');                
                clearInterval(stop);                
                var  count1  =  30;                
                function  b()  {                    
                    count1--;                    
                    console.log(count1);                    
                    if  (count1  ==  0)  {                        
                        console.log('黄灯到了 ');                        
                        clearInterval(stop1);                        
                        var  count2  =  3;

                        function  c()  {                            
                            count2--;                            
                            console.log(count2);                            
                            if  (count2  ==  0)  {                                
                                console.log('红灯到了 ');                                
                                clearInterval(stop2);                                
                                var  count3  =  30;

                                                                
                                function  d()  {                                    
                                    count3--;                                    
                                    console.log(count3);                                    
                                    if  (count3  ==  0)  {                                        
                                        console.log('绿灯到了 ');                                        
                                        clearInterval(stop3);                                         // var count = 8;
                                        count  =  30;                                        
                                        stop  =  setInterval(a,  100);                                    
                                    }                                
                                }                            
                            }                            
                            var  stop3  =  setInterval(d,  100)                        
                        }                        
                        var  stop2  =  setInterval(c,  100)                    
                    }                
                }                
                var  stop1  =  setInterval(b,  100)            
            }        
        }
        var btn = document.querySelector('button');
        btn.onclick = function() {
            clearInterval(stop)
        }

    </script>
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值