JS实现中午吃什么[Math对象+定时器(setInterval)]

JS实现中午吃什么
Math对象+定时器(setInterval)

html部分

	<div class="title">中午吃什么?</div><br><br>
	<div class="eat"></div><br>
	<div class="start block">开始</div>
	<div class="end block">停止</div>

css部分

		*{
            margin: 0;
            padding: 0;
        }
        body{
            background: #eee;
        }
        .title{
            margin-top: 200px;
            font-size: 30px;
            text-align:center;
        }
        .eat{
            font-size: 34px;
            font-weight: 900;
            color: red;
            text-align: center;
        }
        .block{
            width: 160px;
            border: 1px solid black;
            font-size: 20px;
            text-align: center;
            line-height: 40px;
            margin: 0 auto;
        }
        .end{
            display: none;
        }

js部分

	<script>
        let arr = ['馄饨','拉面','烩面','热干面','刀削面','油泼面','炸酱面','炒面','重庆小面','米线','酸辣粉','土豆粉','螺狮粉','凉皮儿','麻辣烫','肉夹馍','羊肉汤','炒饭','盖浇饭','卤肉饭','烤肉饭','黄焖鸡米饭','驴肉火烧','川菜','麻辣香锅','火锅','酸菜鱼','烤串','披萨','烤鸭','汉堡','炸鸡','寿司','蟹黄包','煎饼果子','生煎','炒年糕'];
        let eat = document.querySelector('.eat');
        let start = document.querySelector('.start');
        let end = document.querySelector('.end');
        let timer = null;
        let i = 0;

        start.onclick = function(){
            ++i;
            start.style.display = 'none';
            end.style.display = 'block';
            if(i == 4){
                alert('这么作,别吃了');
                start.style.display = 'none';
                end.style.display = 'none'
            }else{
                timer = setInterval(()=>{
                	//random_num随机数
                	//Math.floor 向下取整
                	//Math.random() 随机数
                	//Math.random()*(最大值-最小值+1)+最大值  得到最小值到最大值范围内的数值
                    let random_num = Math.floor(Math.random() * ((arr.length - 1) + 1));
                    eat.innerText = arr[random_num];
                },50)
            }
        }
        end.onclick = function(){
            start.style.display = 'block';
            start.innerText = '不行,换一个'
            end.style.display = 'none';
            clearInterval(timer); //取消定时器
            timer = null;
        }

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值