<!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>
.box {
width: 500px;
height: 300px;
margin: 100px auto;
}
.title {
display: flex;
width: 500px;
height: 100px;
}
.title span {
margin: 20px;
}
button {
margin: 30px;
}
</style>
</head>
<body>
<div class="box">
<div class="title">
<span>随机点名:</span>
<span class="qs">显示的名字</span>
</div>
<div class="btn">
<button>开始</button>
<button>结束</button>
</div>
</div>
</body>
</html>
<script>
let arr = ['小花', '小丽', '小刚', '小明', '小华', '小华', '小晓']
let qs = document.querySelector('.qs')
let start = document.querySelector('.btn button:nth-child(1)')
let end = document.querySelector('.btn button:nth-child(2)')
let timer
let randomIndex
start.addEventListener('click',function() {
clearInterval(timer)
timer = setInterval(function() {
randomIndex = Math.floor(Math.random() * arr.length)
qs.innerHTML = arr[randomIndex]
},100)
})
end.addEventListener('click',function () {
clearInterval(timer)
arr.splice(randomIndex, 1)
console.log(arr);
if (arr.length === 0) {
start.disabled = true
end.disabled = true
}
})
</script>