先来写HTML代码和CSS样式
<style>
*{
margin: 0;
padding: 0;
}
.box{
width: 600px;
height: 400px;
border: 1px solid black;
}
.box .one{
width: 200px;
height: 30px;
border: 1px solid yellow;
}
.box .two{
width: 200px;
height: 60px;
border: 1px solid aqua;
overflow: auto;
}
</style>
<div class="box">
<div class="one" id="one"></div>
<button id="start">开始</button>
<button id="end">停止</button><br>
</div>
然后是javascript代码,实现功能
var data=['张三','李四','熊大','光头强','李白','姬小满','韩信','王五']
start.onclick=function(){
start.setAttribute('disabled','true')//禁用
var timer1=setInterval(function(){
// 生成0到7的整数
var number=Math.round(Math.random()*7+0)
one.innerHTML=`${data[number]}`
end.onclick=function(){
clearInterval(timer1)
start.removeAttribute('disabled')//解除
}
},100)
}