<html>
<head>
<title>js随机抽奖一二三等奖不重复的抽奖逻辑怎么设置</title>
<meta charset="UTF-8" />
<script>
//添加一个随机函数
Math.rand = function(min, max){
function subRand(min, max){
min = min ? min : 0
max = max ? max : 9
var result = 0
do{
result = Math.random().toString().substr(2, 1)
}while(!(result >= min && result <= max))
return result
}
function getBit(maxBit){
maxBit = maxBit ? maxBit : max.toString().length
var result = [],
count = 0
for(var i = 0 i < maxBit i++){
result.push(subRand())
if(i != 0 && result[i] == result[i - 1]){
count++
}
}
return maxBit - count
}
min = min ? min : 0
max = max ? max : 0
var result = '',
bit = getBit()
do{
result = ''
for(var i = 0 i < bit i++){
result = result + subRand()
}
result = parseInt(result)
}while(!(result >= min && result <= max))
return result
}
//console.log(Math.rand(0, 100))
//LuckDraw 抽奖类 参数一 奖池数组, 参数二 中奖数量 返回 中奖索引
function LuckDraw(pool, numberOfWinners){
var results = [],
//是否已经中奖
isExists = function(index){
for(var i = 0 i < results.length i++){
if(results[i] == index){
return true
}
}
return false
},
subLuckDraw = function(){
do{
result = Math.rand(0, pool.length - 1)
}while(isExists(result))
return result
}
for(var i = 0 i < numberOfWinners i++){
results.push(subLuckDraw())
}
return results
}
var pool = [
'关',
'张',
'赵',
'马',
'黄',
'曹老板'
]
winners = LuckDraw(pool, 3) //返回从奖池中 中奖的索引,假设123等奖都只有一名的话各取一个即可
//假设123等奖 一等奖 1名, 二等奖 2名, 三等奖 3名, 只需要调用这个函数第二个参数设置为总和 6 即可
//然后从结果中第一个索引为一等奖 23 索引为二等奖 , 456索引为 三等奖
console.log(winners)
</script>
</head>
<body>
<script>
for(var i = 0 i < winners.length i++){
document.write(pool[winners[i]] + ' 恭喜你中了' + (i + 1) + '等奖<br />')
}
</script>
</body>
</html>
用一个setinterval方法就搞定了
请点击输入图片描述
代码(用了JQuery):
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<script id="script" type="text/javascript" src="https://code.jquery.com/jquery-3.4.1.min.js"></script>
</head>
<body>
<input id="C" type="" name=""><button id="sta">Start</button>
<input id="E" type="" name=""><button id="sto">Stop</button>
<script type="text/javascript">
var arr = ['A','B','C','D','E']
function getR(){
return Math.floor(Math.random()*arr.length)
}
var t=0
$('#sta').click(function(){
t=window.setInterval(function(){
let tt = arr[getR()]
$('#C').val(tt)
},300)
})
$('#sto').click(function(){
window.clearInterval(t)
$('#E').val($('#C').val())
})
</script>
</body>
</html>
IE打开===================
<html>
<head>
<meta http-equiv="Content-Language" content="zh-cn">
<meta http-equiv="Content-Type" content="text/htmlcharset=gb2312">
<title>Js模拟抽奖的效果</title>
</head>
<body>
<style type="text/css">
.a1{
position:relative
font-family:Verdana
font-size:20px
color:#889978
}
</style>
<script language="JavaScript">
function lotto(){
if (!document.all &&!document.layers)
return
for (j=1j <48j++)//stop zero!?
{
var a=Math.round(Math.random()*j+1)
var b=Math.round(Math.random()*j+1)
var c=Math.round(Math.random()*j+1)
var d=Math.round(Math.random()*j+1)
var e=Math.round(Math.random()*j+1)
var f=Math.round(Math.random()*j+1)
}
var x=new Array(a,b,c,d,e,f)
if ((x[0] == x[1]) || (x[0] == x[2]) || (x[0] == x[3]) || (x[0] == x[3]) || (x[0] == x[5]) ||
(x[1] == x[2]) || (x[1] == x[3]) || (x[1] == x[4]) || (x[1] == x[5]) ||
(x[2] == x[3]) || (x[2] == x[4]) || (x[2] == x[5]) ||
(x[3] == x[4]) || (x[3] == x[5]) ||
(x[4] == x[5]) || (x[5] == x[4]))
lotto()//restart
else
{
var y=x.toString()
var z=y.split(',')
for (i=0i <z.lengthi++)
{
z[i]=z[i]+" "
if (z[i].length <3)//add '0' to singles
z[i]="0"+z[i]+" "
}
z=z.sort()
Nos=z[0]+z[1]+z[2]+z[3]+z[4]+z[5]
if (document.all)
{document.all.layer1.innerHTML=Nos}//OR Z!
else if (document.layers)
{
document.layers.layer1.document.open()
document.layers.layer1.document.write("<span style='position:absolutetop:0pxleft:0pxfont-family:Verdanafont-size:20pxcolor:#888888text-align:center'>"+Nos+"</span>")
document.layers.layer1.document.close()
}
T=setTimeout('lotto()',10)
}
}
//-->
</script>
<table border='0' width=250 height=50>
<tr valign='middle'>
<td align='center'>
<form name=form>
<input type=button value='点击看看你有没有中将' onClick="lotto(),setTimeout('clearTimeout(T)',3000)">
</form>
<span id=layer1 class=a1>Result</span>
</td>
</tr>
</table>
</body>
</html>