Easily create a simple lottery system with JavaScript _javascript tips

Source: Internet
Author: User
Tags setinterval
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" > <ptml> <pead> <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 = Docume Nt.getelementbyid ("Result"). Value; cellnum++; } </script> </pead> <body> &Lt;center> <div> </div> <div id= "main" > <div> <p> winning number </p> <p> & Lt;input id= "Result" type= "text" size= "style=" height:130px;width:800px;border:2px solid; " readonly/></p> <p> <input id= "Start" type= "button" "value=" Started "style=" border:1px solid; Border-color: #aaa #000 #aaa; width:4em; Background: #fc0; "onclick=" SetTimer () "/> <input id=" End "type=" button "value=" Stop "style=" border:1px solid; Border-color: #aaa #000 #aaa; width:4em; Background: #fc0; " Onclick= "Cleartimer () setvalues ();" Disabled/> </p> <p><strong> First prize (10 names) </strong></p > <table width= "946" height= "3" border= "1" > <tr> <td><input name= "text36" type= "text" id= "(+ +) 6 "style=" height:30px;width:190px;border:1px solid red;font-size:25; "Size=" readonly/></td> <td>& Lt;input name= "Text37" type= "text" id= "Notoginseng" style= "Height:30px;width:1"90px;border:1px solid red;font-size:25 size= "readonly/></td> <td><input" name= "Text38" t Ext "id=" "style=" height:30px;width:190px;border:1px solid red;font-size:25; "Size=" readonly/></td> &L T;td><input name= "text39" type= "text" id= "," style= "height:30px;width:190px;border:1px solid" red;font-size:25 "Size=" readonly/></td> <td><input name= "TEXT40" type= "text" id= "style=" height:30px;width : 190px;border:1px solid red;font-size:25; "size=" readonly/></td> </tr> <tr> <td>&lt ; input name= "text41" type= "text" id= "" style= "height:30px;width:190px;border:1px solid;" Red;font-size:25 "28" readonly/></td> <td><input name= "Text42" type= "text" id= "" style= "height:30px;width:190px; border:1px solid red;font-size:25 size= readonly/></td> <td><input name= "text43" type= "text" I D= "style=" height:30px;width:190px;border:1PX Solid red;font-size:25 "size=" readonly/></td> <td><input name= "Text44" type= "text" id= "s" Tyle= "height:30px;width:190px;border:1px solid red;font-size:25; size=" Readonly/></td> <td>< The input name= "text45" type= "text" id= "style=" height:30px;width:190px;border:1px solid; red;font-size:25 "20" Readonly/></td> </tr> </table> <p> Second Prize (15) </p> <table width= "951" height= "88" border= "1" > <tr> <td><input name= "text21" type= "text" id= "style=" Height:30px;width:190px;borde r:1px solid red;font-size:25 "size=" readonly/></td> <td><input name= "text22" type= "text" id= "22 "Style=" height:30px;width:190px;border:1px solid red;font-size:25; "Size=" readonly/></td> <td>& Lt;input name= "text23" type= "text" id= "" style= "height:30px;width:190px;border:1px solid;" Red;font-size:25 " "Readonly/></td> &LT;TD><input name= "Text24" type= "text" id= "" style= "height:30px;width:190px;border:1px solid;" Size= "readonly/></td>" <td><input name= "Text25" type= "text" id= "" style= "height:30px;width : 190px;border:1px solid red;font-size:25; "size=" readonly/></td> </tr> <tr> <td>&lt ; input name= "Text26" type= "text" id= "style=" height:30px;width:190px;border:1px solid; "Red;font-size:25" 28 " readonly/></td> <td><input name= "Text27" type= "text" id= "" style= "height:30px;width:190px; border:1px solid red;font-size:25 size= readonly/></td> <td><input name= "Text28" type= "text" I D= "style=" height:30px;width:190px;border:1px solid red;font-size:25; "Size=" readonly/></td> <td& Gt;<input name= "Text29" type= "text" id= "" style= "height:30px;width:190px;border:1px solid;" Size= "Readonly/></td>" <td><input Name= "Text30" type= "text" id= "style=" height:30px;width:190px;border:1px solid; "Red;font-size:25" 20 " readonly/></td> </tr> <tr> <td><input name= "text31" type= "text" id= "" style= "Height: 30px;width:190px;border:1px solid red;font-size:25, "size=" readonly/></td> <td><input " Text32 "type=" text "id=" "style=" height:30px;width:190px;border:1px solid; "Red;font-size:25" size= ></td> <td><input name= "text33" type= "text" id= "style=" height:30px;width:190px;border:1px Solid red;font-size:25 "size=" readonly/></td> <td><input name= "Text34" type= "text" id= "Styl" E= "height:30px;width:190px;border:1px solid red;font-size:25; size=" Readonly/></td> <td>< The input name= "Text35" type= "text" id= "style=" height:30px;width:190px;border:1px solid; "Red;font-size:25" 20 " Readonly/></td> </tr> </table> &Lt;p> Third Prize (20) </p> <table width= "961" height= "102" border= "1" > <tr> <td><input name= "Te XT1 "type=" text "id=" 1 "style=" height:30px;width:190px;border:1px solid; red;font-size:25 "Size=" </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:3 0px;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=" lt;/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= "Te XT "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" s Tyle= "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 "20" 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= "10" Style= "height:30px;width:190px;border:1px solid red;font-size:25; size=" Readonly/></td> </tr> & lt;tr> <td><input name= "text11" type= "text" id= "one style=" height:30px;width:190px;border:1px solid red; Font-size:25 "size=" "Readonly/></td> <td><input name=" text12 "type=" text "id=" "style=" height : 30px;width:190px;border:1px Solid Red;font-Size:25 "size=" readonly/></td> <td><input name= "text13" type= "text" id= "style=" height:30p x;width:190px;border:1px solid red;font-size:25; "size=" readonly/></td> <td><input "Name=" Text14 "type=" text "id=" "style=" height:30px;width:190px;border:1px solid; "Red;font-size:25" size= ></td> <td><input name= "text15" type= "text" id= "style=" height:30px;width:190px;border:1px Solid red;font-size:25 "size=" readonly/></td> </tr> <tr> <td><input name= "Text1" 6 "type=" text "id=" "style=" height:30px;width:190px;border:1px solid red;font-size:25; "Size=" readonly/>< /TD> <td><input name= "text17" type= "text" id= "" style= "height:30px;width:190px;border:1px solid red; Font-size:25 "size=" readonly/></td> <td><input name= "text18" type= "text" id= "" style= "height : 30px;width:190px;border:1px solid red;font-size:25; "Size= "readonly/></td> <td><input name=" text19 "type=" text "id=" "style=" height:30px;width : 190px;border:1px solid red;font-size:25 size= "readonly/></td> <td><input name=" text20 "Text" id= "style=" height:30px;width:190px;border:1px solid red;font-size:25; "Size=" readonly/></td> </tr> </table> <p> </p> <p> </p> <p> </p> </div> </div > <center> </body> </ptml>
[Ctrl + A All SELECT Note: If the need to introduce external JS need to refresh to perform]
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.