Easy to create a simple lottery system with JavaScript

Source: Internet
Author: User
Tags array min return setinterval
Javascript

Author: Jegg
At the end of the year, many companies are busy with the annual meeting, there will be some lottery activities, the following program is written in JavaScript with a simple lottery system to share with you.

This code borrows some of the online user's code, plus some improvements such as not repeating the lottery. The general idea is as follows:

1. Put all the lottery data (here for mobile phone number) into the array.
2. Using the random function to randomly generate the index of the array
3. Use the SetInterval function to produce an array of random index corresponding to the cell phone number at very short intervals and display.
4. Use the Removeeleat (index) function to remove randomly generated phone numbers. and re-organize to generate the remaining phone number for a new array for next use.

How to use:

Copy the following original code to WordPad and then the TXT attribute to HTML. If the display is garbled, please change the page encoding (view-code).

The original code is as follows:


<! DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 transitional//en" >
<title> 2006-Year lottery system </title>
<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 ">
<script language= "JavaScript" >

Global variables
var timer;
var flag = new Array (100);
var existingnum = new Array (100);
var clicktimes = 0;
var randnum;
var cellnum = 1;
var mobile = new Array ();
Set Data here!!
mobile[0]=13020000100;
mobile[1]=13020000101;
mobile[2]=13020000102;
mobile[3]=13020000103;
mobile[4]=13020000104;
mobile[5]=13020000105;
mobile[6]=13020000106;
mobile[7]=13020000107;
mobile[8]=13020000108;
mobile[9]=13020000109;
mobile[10]=13020000110;
mobile[11]=13020000111;
mobile[12]=13020000112;
mobile[13]=13020000113;
mobile[14]=13020000114;
mobile[15]=13020000115;


mobile[16]=13020000116;
mobile[17]=13020000117;
mobile[18]=13020000118;
mobile[19]=13020000119;
mobile[20]=13020000120;
mobile[21]=13020000121;
mobile[22]=13020000122;
mobile[23]=13020000123;
mobile[24]=13020000124;
mobile[25]=13020000125;
mobile[26]=13020000126;
mobile[27]=13020000127;
mobile[28]=13020000128;
mobile[29]=13020000129;
mobile[30]=13020000130;
mobile[31]=13020000131;
mobile[32]=13020000132;
mobile[33]=13020000133;
mobile[34]=13020000134;
mobile[35]=13020000135;
mobile[36]=13020000136;
mobile[37]=13020000137;
mobile[38]=13020000138;
mobile[39]=13020000139;
mobile[40]=13020000140;
mobile[41]=13020000141;
mobile[42]=13020000142;
mobile[43]=13020000143;
mobile[44]=13020000144;
mobile[45]=13020000145;
mobile[46]=13020020146;
var num = mobile.length-1;
function Getrandnum () {
document.getElementById ("Result"). Value = Mobile[getrnd (0,num)];
}
function Start () {
Clearinterval (timer);
Timer = setinterval (' Change () ', 50);
}
function ok () {
Clearinterval (timer);
}
function Getrnd (Min,max) {

Randnum = parseint (Math.random () * (max-min+1));
return randnum;
}
function SetTimer () {
Timer = setinterval ("Getrandnum ();", 10);
document.getElementById ("Start"). Disabled = true;
document.getElementById ("End"). Disabled = false;
}
function Cleartimer () {
Nodupnum ();
Clearinterval (timer);
document.getElementById ("Start"). Disabled = false;
document.getElementById ("End"). Disabled = true;

}


function Nodupnum () {
To remove the selected mobile phone number
Mobile.removeeleat (Randnum);

To reorganize the mobile number array!!
var o = 0;
for (P = p<mobile.length;p++) {
if (typeof mobile[p]!= "undefined") {
Mobile[o] = mobile[p];
o++;
}
}
num = mobile.length-1;
}
method to remove the element in the array
Array.prototype.removeEleAt = function (dx)
{
if (isNaN (dx) | | Dx>this.length) {return false;}
This.splice (dx,1);
}
Set mobile phone numbers to the table cell
function Setvalues () {
document.getElementById (cellnum). Value = document.getElementById ("result"). Value;
cellnum++;
}
</script>
<body>
<center>
<div> </div>
<div id= "Main" >
<div>

<p>
<input id= "Result" type= "text" size= "style=" height:130px;width:800px;border:2px solid; " Readonly/></p>
<p>
<input id= "Start" type= "button" value= "Start" style= "border:1px solid; Border-color: #aaa #000 #aaa; width:4em; Background: #fc0; "/>
<input id= "End" type= "button" value= "Stop" style= "border:1px solid; Border-color: #aaa #000 #aaa; width:4em; Background: #fc0; "disabled/>
</p>
<p><strong> First Prize (10 names) </strong></p>
<table width= "946" height= "border=" "1" >
<tr>
<td><input name= "Text36" type= "text" id= "" style= "height:30px;width:190px;border:1px solid Red;font-size : "Size=" readonly/></td>
<td><input name= "Text37" type= "text" id= "Notoginseng" style= "height:30px;width:190px;border:1px solid Red;font-size : size= "Readonly/></td>"
<td><input name= "Text38" type= "text" id= "-" style= "height:30px;width:190px;border:1px solid Red;font-size : size= "Readonly/></td>"
<td><input name= "text39" type= "text" id= "," style= "height:30px;width:190px;border:1px solid" red;font-size : size= "Readonly/></td>"
<td><input name= "TEXT40" type= "text" id= "-" style= "height:30px;width:190px;border:1px solid Red;font-size : size= "Readonly/></td>"
</tr>
<tr>
<td><input name= "text41" type= "text" id= "a" style= "height:30px;width:190px;border:1px solid Red;font-size : "Size=" readonly/></td>
<td><input name= "Text42" type= "text" id= "," style= "height:30px;width:190px;border:1px solid Red;font-size : size= "Readonly/></td>"
<td><input name= "text43" type= "text" id= "" style= "height:30px;width:190px;border:1px solid Red;font-size : size= "Readonly/></td>"
<td><input name= "Text44" type= "text" id= "" style= "height:30px;width:190px;border:1px solid. Red;font-size : size= "Readonly/></td>"
<td><input name= "Text45" type= "text" id= "" style= "height:30px;width:190px;border:1px" Solid red;font-size : size= "Readonly/></td>"
</tr>
</table>
<p> Second Prize (15) </p>
<table width= "951" height= "border=" 1 ">
<tr>


<td><input name= "text21" type= "text" id= "" style= "height:30px;width:190px;border:1px solid Red;font-size : "Size=" readonly/></td>
<td><input name= "text22" type= "text" id= "" style= "height:30px;width:190px;border:1px solid Red;font-size : size= "Readonly/></td>"
<td><input name= "text23" type= "text" id= "" style= "height:30px;width:190px;border:1px solid Red;font-size : size= "Readonly/></td>"
<td><input name= "Text24" type= "text" id= "" style= "height:30px;width:190px;border:1px solid Red;font-size : size= "Readonly/></td>"
<td><input name= "Text25" type= "text" id= "" style= "height:30px;width:190px;border:1px solid Red;font-size : size= "Readonly/></td>"
</tr>
<tr>
<td><input name= "Text26" type= "text" id= "" style= "height:30px;width:190px;border:1px solid Red;font-size : "Size=" readonly/></td>
<td><input name= "Text27" type= "text" id= "" style= "height:30px;width:190px;border:1px solid Red;font-size : size= "Readonly/></td>"
<td><input name= "Text28" type= "text" "Id=" "style=" height:30px;width:190px;border:1px solid Red;font-size : size= "Readonly/></td>"
<td><input name= "Text29" type= "text" id= "" style= "height:30px;width:190px;border:1px solid Red;font-size : size= "Readonly/></td>"
<td><input name= "Text30" type= "text" id= "" style= "height:30px;width:190px;border:1px solid Red;font-size : size= "Readonly/></td>"
</tr>
<tr>
<td><input name= "Text31" type= "text" id= "to" style= "height:30px;width:190px;border:1px solid Red;font-size : "Size=" readonly/></td>
<td><input name= "Text32" type= "text" id= "" style= "height:30px;width:190px;border:1px solid Red;font-size : size= "Readonly/></td>"
<td><input name= "Text33" type= "text" id= "" style= "height:30px;width:190px;border:1px solid Red;font-size : size= "Readonly/></td>"
<td><input name= "Text34" type= "text" id= "" style= "height:30px;width:190px;border:1px solid Red;font-size : size= "Readonly/></td>"
<td><input name= "Text35" type= "text" id= "a" style= "height:30px;width:190px;border:1px solid Red;font-size : size= "Readonly/></td>"
</tr>
</table>
<p> Third Prize (20 names) </p>
<table width= "961" height= "102" border= "1" >
<tr>
<td><input name= "Text1" type= "text" id= "1" style= "height:30px;width:190px;border:1px solid red;font-size:25 ; "Size=" readonly/></td>
<td><input name= "Text2" type= "text" id= "2" style= "height:30px;width:190px;border:1px solid red;font-size:25 ; "Size=" readonly/></td>
<td><input name= "Text3" type= "text" id= "3" style= "height:30px;width:190px;border:1px solid red;font-size:25 ; "Size=" readonly/></td>
<td><input name= "Text4" type= "text" id= "4" style= "height:30px;width:190px;border:1px solid red;font-size:25 ; "Size=" readonly/></td>
<td><input name= "Text5" type= "text" id= "5" style= "height:30px;width:190px;border:1px solid red;font-size:25 ; "Size=" readonly/></td>
</tr>
<tr>
<td><input name= "Text6" type= "text" id= "6" style= "height:30px;width:190px;border:1px solid red;font-size:25 ; "Size=" readonly/></td>
<td><input name= "Text7" type= "text" id= "7" style= "height:30px;width:190px;border:1px solid red;font-size:25 ; "Size=" readonly/></td>
<td><input name= "Text8" type= "text" id= "8" style= "height:30px;width:190px;border:1px solid red;font-size:25 ; "Size=" readonly/></td>
<td><input name= "Text9" type= "text" id= "9" style= "height:30px;width:190px;border:1px solid red;font-size:25 ; "Size=" readonly/></td>
<td><input name= "text10" type= "text" id= "" style= "height:30px;width:190px;border:1px solid Red;font-size : size= "Readonly/></td>"
</tr>
<tr>
<td><input name= "text11" type= "text" id= "one" style= "height:30px;width:190px;border:1px solid Red;font-size : "Size=" readonly/></td>
<td><input name= "text12" type= "text" id= "" style= "height:30px;width:190px;border:1px solid Red;font-size : size= "Readonly/></td>"
<td><input name= "text13" type= "text" id= "" style= "height:30px;width:190px;border:1px solid Red;font-size : size= "Readonly/></td>"
<td><input name= "text14" type= "text" id= "" style= "height:30px;width:190px;border:1px" Solid red;font-size : size= "Readonly/></td>"
<td><input name= "text15" type= "text" id= "" style= "height:30px;width:190px;border:1px solid Red;font-size : size= "Readonly/></td>"
</tr>
<tr>
<td><input name= "Text16" type= "text" id= "" style= "height:30px;width:190px;border:1px solid Red;font-size : "Size=" readonly/></td>
<td><input name= "text17" type= "text" id= "" style= "height:30px;width:190px;border:1px solid Red;font-size : size= "Readonly/></td>"
<td><input name= "text18" type= "text" id= "" style= "height:30px;width:190px;border:1px solid Red;font-size : size= "Readonly/></td>"
<td><input name= "text19" type= "text" id= "" style= "height:30px;width:190px;border:1px solid Red;font-size : size= "Readonly/></td>"
<td><input name= "text20" type= "text" id= "" style= "height:30px;width:190px;border:1px solid Red;font-size : size= "Readonly/></td>"
</tr>
</table>
<p> </p>
<p> </p>
<p> </p>
</div>
</div>
<center>
</body>


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.