代码描述:js不重复抽奖。js不重复抽奖代码实例
本章节详细分析一段代码实例,它实现了简单的随机抽奖效果,也可以称之为随机点名效果。 此效果的一个很重要的特点就是具有随机性,并且不能出现重复现象。 代码实例如下:
<!DOCTYPE html> <html> <head> <meta charset=" utf-8"> <meta name="author" content="https://www.51qianduan.com/" /> <title>51前端</title> <style> #box{ text-align:center; margin-top:100px; width:100%; } #show{ margin:auto; font-size:50px; width:300px; height:50px; background:#FFEEFF; } </style> <script type="text/javascript"> var isRun=true; var arr=["张三","李四","王五","赵六","51前端","antzone","softwhy.com"]; var old=[]; function action(){ var btValue=document.getElementById("bt").value; if(btValue=="开始"){ isRun=true; run(); document.getElementById("bt").value="结束"; } else{ isRun=false; document.getElementById("bt").value="开始"; } } function run(){ var index=Math.floor(Math.random() * arr.length); document.getElementById("show").innerHTML=arr[index]; if(isRun==false){ var b =true; for(var j in old){ if(old[j]==index){ b=false; } } if(b){ old[old.length]=index; return; } } setTimeout("run()",10); } window.onload=function(){ var obt=document.getElementById("bt"); obt.onclick=function(){ action() } } </script> </head> <body> <div id="box" style=""> <div id="show"></div> <div style="margin-top:20px;"> <input id="bt" type="button" value="开始"/> </div> </div> </body> </html>
请绑定手机号,在继续操作
注意:只有绑定手机以后,才可使用网站全部功能