<html>
<head>
<meta charset="utf-8"/>
<meta name="format-detection" content="telephone=no">
<title>test</title>
<style type="text/css">
body{text-align: center}
.div1{
max-width: 1000px
margin: 50px auto
padding: 20px 0
background-color: #efa
}
</style>
</head>
<body>
<div class="div1">试试手气</div>
<button class="btn">点击抽奖</button>
<script type="text/javascript">
window.onload=function(){
// var form=document.forms[0]
var oDiv=document.querySelector(".div1")
var oBtn=document.querySelector(".btn")
var arr=[
{item:"美女一个",chances:20},
{item:"巴掌一个",chances:15},
{item:"嗜血珠",chances:20},
{item:"摄魂棒",chances:20},
{item:"诛仙剑",chances:25}
] //奖项对象
// 处理数据
var start_num=0
for(var i=0,len=arr.lengthi<leni++){
if(i!=0){
start_num+=arr[i-1]["chances"]
}
arr[i].range=[start_num,(start_num+arr[i]["chances"]-1)]
}
oBtn.onclick=function(){ //绑定事件
setFunc()
}
console.log(arr)
function setFunc(){
var num=parseInt(Math.random()*100)
for(var j=0,len=arr.lengthj<lenj++){
if(arr[j]["range"][0]<=num && arr[j]["range"][1]>=num){
oDiv.innerHTML="恭喜你获得"+arr[j]["item"]
}
}
}
}
</script>
</body>
</html>
写了个例子,可以参考一下思路
msg2的onclick改为:onclick="showCustomer(this.d)setTimeout(CountDown()',3000)"
3秒后重新执行CountDown()。如果需要立即执行:
onclick="showCustomer(this.d)CountDown()"
如有问题欢迎继续交流