<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<div
style="width: 300px;height: 200px;background-color: grey;font-size: 50px;text-align: center;line-height: 200px;">
随机点名</div>
<button style="width: 300px;text-align: center;height: 100px;font-size: 50px">开始</button>
<br>
<button style="width: 300px;text-align: center;height: 100px;font-size: 50px">停止</button>
<script>
var arr = ["张三", "李四", "王麻子", "狗蛋", "狗剩"];
var bt = document.getElementsByTagName("button");
var div = document.getElementsByTagName("div")[0];
var timer
bt[0].onclick = function () {
timer = setInterval(function () {
var index = parseInt(Math.random() * arr.length);
div.innerHTML = arr[index];
}, 50)
}
bt[1].onclick = function () {
clearInterval(timer);
}
</script>