效果图如下:
<style>
h3{
text-align: center;
}
button{
outline: none;
border:none;
padding:10px 30px;
margin-right: 10px;
background:pink;
color:#fff;
cursor: pointer;
}
</style>
<h3 id="box">谁会是幸运儿?</h3>
<button id="startBtn">开始</button><button id="stopBtn">暂停</button><button id="resetBtn">重置</button>
<script>
window.onload = function(){
let arr = ['刘海生','猜猜','菜菜','花花','果果','妞妞','溜溜','六六','嘻嘻','哈哈','张张','来来','三丰'];
let timer = null;
function getNum(min,max){
let num = Math.floor(Math.random()*(max-min)+min);
return num
}
//开始按钮
startBtn.onclick = function(){
clearInterval(timer) //每次点击的时候清除定时器,防止多次点击导致越来越快
timer = setInterval(function(){
box.innerHTML = arr[getNum(0,arr.length)]
},300)
}
//暂停按钮
stopBtn.onclick = function(){
clearInterval(timer)
}
//重置按钮
resetBtn.onclick = function(){
clearInterval(timer)
box.innerHTML = "谁会是幸运儿?"
}
}
</script>