js写的小抽奖 随机出
<body>
<div>名字出现区域</div>
<button id="btn1">开始</button>
<button id="btn2">停止</button>
<script type="text/javascript">
//获取HTML里对应的标签
var div=document.getElementsByTagName("div")[0]
var btn1=document.getElementById("btn1")
var btn2=document.getElementById("btn2")
//该数组存放能被抽出来的名字
var arr=["张三","李四","王五","赵六","一一","尔尔"]
//让名字随机出现并显示在圆形中
function fun(){
var num=Math.floor(Math.random()*6)
div.innerText=arr[num]
}
//下面是2种写法 2个按钮分别是开始和停止
// btn1.onclick=function(){
// timer=setInterval(fun,100)
// }
// btn2.onclick=function(){
// clearInterval(timer)
// alert("恭喜"+div.innerText+"中奖")
// }
btn1.addEventListener("click",function(){
timer=setInterval(fun,100)
})
btn2.addEventListener("click",function(){
clearInterval(timer)
alert("恭喜"+div.innerText+"中奖")
})
</script>
</body>