JavaScript Simple lottery program Implementation and code

Source: Internet
Author: User

JavaScript Simple lottery program Implementation and code

1. Requirements Description

A company year-end lottery, need to have the following functions

1) The maximum number of visitors can be set according to the actual situation

2) Click "Start", big screen Scroll, click "Stop", the winner's number appears on the big screen

3) Show all prize winner numbers in the interface

4) do not repeat the award

5) does not cause the lottery result to be abnormal due to input errors.

2. Code Submission

    1. <! DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 transitional//en" >
  • <title> a company anniversary draw </title>
  • <meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 ">
  • <script language= "JavaScript" >
  • var timer;
  • var flag = new Array (100);
  • var existingnum = new Array (100);
  • var clicktimes = 0;
  • var randnum;
  • var cellnum = 1;
  • var mobile;
  • var num;
  • function Check_input () {
  • var input = document.getElementById ("Real_num"). Value;
  • var re =/^[1-9]+[0-9]*]*$/;
  • if (!re.test (input)) {
  • Alert ("Please enter a positive integer");
  • Window.location.href=window.location.href;
  • return false;
  • }
  • }
  • Get the random numbers from the mobile array every 0.05s
  • function SetTimer () {
  • Timer = setinterval ("Getrandnum ();", 50);
  • document.getElementById ("Start"). Disabled = true;
  • document.getElementById ("End"). Disabled = false;
  • }
  • function Getrandnum () {
  • document.getElementById ("Result"). Value = Mobile[getrnd (0,num)];
  • }
  • function Getrnd (Min,max) {
  • Randnum = parseint (Math.random () * (max-min+1));
  • return randnum;
  • }
  • //------------------------------------------------
  • Turn the input ' s running down
  • function Cleartimer () {
  • Nodupnum ();
  • Clearinterval (timer);
  • document.getElementById ("Start"). Disabled = false;
  • document.getElementById ("End"). Disabled = true;
  • }
  • Re defined Array:change The length of the array and delete the checked one
  • function Nodupnum () {
  • Mobile.removeeleat (Randnum);
  • var o = 0;
  • for (p=0; p<mobile.length;p++) {
  • if (typeof mobile[p]!= "undefined") {
  • Mobile[o] = mobile[p];
  • o++;
  • }
  • }
  • num = mobile.length-1;
  • }
  • function Setvalues () {
  • document.getElementById (cellnum). Value = document.getElementById ("result"). Value;
  • cellnum++;
  • }
  • function Set_array () {
  • var real_num = document.getElementById ("Real_num"). Value;
  • mobile= new Array (Real_num);
  • var o = 0;
  • for (I=1; i<=real_num;i++) {
  • Mobile[o] = i;
  • o++;
  • }
  • num = mobile.length-1;
  • document.getElementById ("Set_number"). Disabled = true;
  • }
  • Array.prototype.removeEleAt = function (dx) {
  • if (IsNaN (dx) | | Dx>this.length) {return false;}
  • This.splice (dx,1);
  • }
  • </script>
  • <body>
  • <center>
  • <div id= "Main" >
  • <div>
  • <p>
  • <input id= "Result" type= "text" size= "" style= "height:130px;width:800px;border:2px solid red;font-size:120;" Readonly/>
  • </p>
  • <p>
  • <input id= "Start" type= "button" value= "Start" 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 (1 persons) </strong></p>
  • <table width= "height=" border= "1" >
  • <tr>
  • <td><input name= "Text1" type= "text" id= "3" style= "height:30px;width:190px;border:1px solid red;font-size:25 ; "Size=" readonly/></td>
  • </tr>
  • </table>
  • <p> Second Prize (2) </p>
  • <table width= "380" height= "border=" 1 ">
  • <tr>
  • <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= "1" style= "height:30px;width:190px;border:1px solid red;font-size:25 ; "Size=" readonly/></td>
  • </tr>
  • </table>
  • </div>
  • </div>
  • <p></p>
  • <p></p>
  • <div id= "Setter" style= "border:1px solid;width:45em" >
  • <table width= "1" height= "border=" >
  • <tr>
  • Number of <td> activities </td>
  • <td><input type= "text" id= "Real_num" style= "Width:11em" ></td>
  • <td><input id= "Set_number" type= "button" value= "Set" style= "border:1px solid; Border-color: #aaa #000 #aaa; width:4em; Background: #fc0; "onclick=" check_input (); Set_array (); " /></td>
  • </tr>
  • <tr>
  • </tr>
  • </table>
  • <br/>
  • <input id= "Set_number" type= "button" value= "re-start the lottery" style= "border:1px solid;  Border-color: #aaa #000 #aaa; Width:45em;height:3em;background: #fc0; " onclick= "WINDOW.LOCATION.HREF=WINDOW.LOCATION.HREF;" />
  • <p></p>
  • </div>
  • <center>
  • </body>

Copy Code

3. Ugly

I am ugly, but I am very gentle ~

JavaScript Simple lottery program Implementation and code

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.