<!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>随机点名(拓展)</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 = ['蔡政', '曹鹏宇', '陈海龙', '凤娇', '郭海昆', '郭锦歌', '金宇', '孔晨阳', '李梦影', '梁露露', '梁夏玲', '刘锦程', '刘强强', '陆鹏飞', '马森', '盛少将', '田子怡', '王方方', '王金松', '王静文', '王永兴', '韦晨阳', '武梦宇', '熊康', '徐倩', '杨付海', '姚杰', '张博翔', '张菲菲', '张凤娇', '张孟杰', '张梦婷', '张润泽', '张志霖', '赵阳', '周欢', '施成'];
function getRandom(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
let qs = document.querySelector('.qs');
let start = document.querySelector('.start');
let end = document.querySelector('.end');
start.addEventListener('click', function() {
timer=setInterval(function() {
random = getRandom(0, arr.length - 1);
qs.innerHTML = arr[random];
}, 30)
if(arr.length===1){
start.disabled=true;
end.disabled=true;
}
})
end.addEventListener('click',function(){
clearInterval(timer);
arr.splice(random,1);
})
</script>
</body>
</html>