彩票小例子

来源:互联网 发布:手机如何连接电脑网络 编辑:程序博客网 时间:2024/04/30 09:22

<!DOCTYPE html>

<html>

<head>

<metacharset="UTF-8">

<title></title>

</head>

<body>

<inputtype="button" name="btn1" id="btn1"value="肯定中奖" />

<div id="div1"></div>

<scripttype="text/javascript">

varoBtn = document.getElementById("btn1");

var oDiv = document.getElementById("div1");

oDiv.innerHTML = "送货上门";

functionrandom(min,max){

returnMath.floor(Math.random() * (max -min + 1) + min);

};

// 产生彩票

functioncaiPiao(){

//创建一个数组,7个元素,随机数范围为【1 - 33】

// 创建一个空数组,用于保存每一个彩票号码

varnums = [];

// 当数组长度为7时,跳出循环

while(nums.length < 7){

//保存每次循环产生的随机数

vartemp =random(1, 33);

//index 下标标识,保证我到底重不重复

varindex = -1;

// 用temp去和数组中每一个元素去比较,如果有相同的,index = 1

for(var i = 0; i< nums.length; i++) {

if(nums[i] == temp) {

index= 1;

}

}

// 如果index == 1,也就是有重复的时候,我不push,没有重复index == -1时,才push

if(index == -1) {

nums.push(temp);

}

}

returnnums;

}

// 摇奖

vartimer = null;

functionyaoHao(){

varstop = 0;

clearInterval(timer);

timer = setInterval(function(){

oDiv.innerHTML= caiPiao();

stop++;

if(stop> 10){

clearInterval(timer);

}

},100);

}

oBtn.onclick= function(){

yaoHao();

}

yaoHao();

</script>

</body>

</html>

0 0
原创粉丝点击