<title>随机点名(拓展)</title>
<style>
* {
margin: 0;
padding: 0;
}
h2,
.btns {
text-align: center;
}
.box {
width: 600px;
margin: 50px auto;
display: flex;
font-size: 25px;
line-height: 40px;
}
.qs {
width: 450px;
height: 40px;
color: red;
}
.btns button {
width: 120px;
height: 35px;
margin: 0 50px;
}
</style>
</head>
<body>
<h2>随机点名</h2>
<div class="box">
<span>名字是:</span>
<div class="qs">这里显示姓名</div>
</div>
<div class="btns">
<button class="start">开始</button>
<button class="end">结束</button>
</div>
<script>
// 准备阶段:声明数组 模拟数据
let arr = ['鲍中义', '陈琳', '常琴娜', '崔文昊', '陈晓丹', '丁克磊', '洞鸣威', '董思雨', '傅玉洁', '高浩', '黄盼月', '黄小龙', '何肖肖', '胡月', '纪宗锋', '刘锦程', '李静怡', '李龙', '陆铭雪', '刘娜', '李晓枫', '马曼婷', '盘进进', '庞雅静', '邱雨嫣', '宋佳豪', '盛也', '孙雨晴', '施雨彤', '陶鑫凯', '武国庆', '王国志', '文静', '温群和', '吴宛励', '汪万元', '王一轩', '王志强', '谢蝶蝶', '徐娟', '袁兴', '余湘湘', '杨雅丽', '张军', '张瑶瑶', '周雨晨'];
let timer = 0;
let random = 0;
// 封装自定义随机函数
function getRandom(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
// 1. 获取元素对象
let qs = document.querySelector('.qs');
let start = document.querySelector('.start');
let end = document.querySelector('.end');
// 2. 给开始按钮注册事件
start.addEventListener('click', function() {
// 利用闹钟定时器 setInterval(处理程序, 毫秒数)
timer = setInterval(function() {
let random = getRandom(0, arr.length - 1);
console.log(arr[random]);
qs.innerHTML = arr[random];
}, 30)
if(arr.length ==1){
start.disabled=true;
}
})
// 3. 给结束按钮注册点击事件
end.addEventListener('click',function(){
clearInterval(timer);
arr.splice(random,1);
console.log(arr);
})
</script>
</body>
</html>
随机点名