JavaScript编写简单的抽奖程序

时间:2023-03-09 17:11:43
JavaScript编写简单的抽奖程序

1.需求说明

某公司年终抽奖,需要有如下功能

1)可以根据实际情况设置到场人数的最大值

2) 点击“开始”,大屏幕滚动,点击“停止”,获奖者的编号出现在大屏幕上

3)在界面里显示全部奖项获奖人编号

4)不重复获奖

5)不会因为输入错误而导致抽奖结果异常。

2.代码呈上

  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 TRANSITIONAL//EN">
  2. <html>
  3. <head>
  4. <title>某公司周年庆抽奖</title>
  5. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  6. <script language="javascript">
  7. var timer;
  8. var flag = new Array(100);
  9. var existingnum = new Array(100);
  10. var clickTimes = 0;
  11. var randnum;
  12. var cellnum =1;
  13. var mobile;
  14. var num ;
  15. function check_input(){
  16. var input = document.getElementById("real_num").value;
  17. var re = /^[1-9]+[0-9]*]*$/;
  18. if (!re.test(input)){
  19. alert("请输入正整数");
  20. window.location.href=window.location.href;
  21. return false;
  22. }
  23. }
  24. //get the random numbers from the mobile array every 0.05s
  25. function setTimer(){
  26. timer = setInterval("getRandNum();",50);
  27. document.getElementById("start").disabled = true;
  28. document.getElementById("end").disabled = false;
  29. }
  30. function getRandNum(){
  31. document.getElementById("result").value = mobile[GetRnd(0,num)];
  32. }
  33. function GetRnd(min,max){
  34. randnum = parseInt(Math.random()*(max-min+1));
  35. return randnum;
  36. }
  37. //------------------------------------------------
  38. //turn the input's running down
  39. function clearTimer(){
  40. noDupNum();
  41. clearInterval(timer);
  42. document.getElementById("start").disabled = false;
  43. document.getElementById("end").disabled = true;
  44. }
  45. // Re defined array:change the length of the array and delete the checked one
  46. function noDupNum(){
  47. mobile.removeEleAt(randnum);
  48. var o = 0;
  49. for(p=0; p<mobile.length;p++){
  50. if(typeof mobile[p]!="undefined"){
  51. mobile[o] = mobile[p];
  52. o++;
  53. }
  54. }
  55. num = mobile.length-1;
  56. }
  57. function setValues(){
  58. document.getElementById(cellnum).value = document.getElementById("result").value ;
  59. cellnum++;
  60. }
  61. function set_array(){
  62. var real_num = document.getElementById("real_num").value ;
  63. mobile= new Array(real_num);
  64. var o = 0;
  65. for(i=1; i<=real_num;i++){
  66. mobile[o] = i;
  67. o++;
  68. }
  69. num = mobile.length-1;
  70. document.getElementById("set_number").disabled = true;
  71. }
  72. Array.prototype.removeEleAt = function(dx){
  73. if(isNaN(dx)||dx>this.length){return false;}
  74. this.splice(dx,1);
  75. }
  76. </script>
  77. </head>
  78. <body>
  79. <center>
  80. <div id="main">
  81. <div>
  82. <h1>获奖小伙伴</h1>
  83. <p>
  84. <input id="result" type="text" size="30" style="height:130px;width:800px;border:2px solid red;font-size:120;" readonly/>
  85. </p>
  86. <p>
  87. <input id="start" type="button" value="开始" style="border: 1px solid; border-color: #aaa 000 #000 #aaa;width:4em; background: #fc0;" onclick="setTimer()" />
  88. <input id="end" type="button" value="停" style="border: 1px solid; border-color: #aaa 000 #000 #aaa;width:4em; background: #fc0;"onclick="clearTimer();setValues();" disabled/>
  89. </p>
  90. <p><strong>一等奖(1名)</strong></p>
  91. <table width="190" height="30" border="1">
  92. <tr>
  93. <td><input name="text1" type="text" id="3" style="height:30px;width:190px;border:1px solid red;font-size:25;" size="28" readonly/></td>
  94. </tr>
  95. </table>
  96. <p>二等奖(2名)</p>
  97. <table width="380" height="30" border="1">
  98. <tr>
  99. <td><input name="text2" type="text" id="2" style="height:30px;width:190px;border:1px solid red;font-size:25;" size="28" readonly/></td>
  100. <td><input name="text3" type="text" id="1" style="height:30px;width:190px;border:1px solid red;font-size:25;" size="20" readonly/></td>
  101. </tr>
  102. </table>
  103. </div>
  104. </div>
  105. <p></p>
  106. <p></p>
  107. <div id="setter" style="border:1px solid;width:45em">
  108. <h3>系统设置</h3>
  109. <table width="300" height="30" border="1">
  110. <tr>
  111. <td>活动人数</td>
  112. <td><input type="text" id="real_num" style="width:11em"></td>
  113. <td><input id="set_number" type="button" value="设置" style="border: 1px solid; border-color: #aaa 000 #000 #aaa;width:4em; background: #fc0;" onclick="check_input();set_array();"/></td>
  114. </tr>
  115. <tr>
  116. </tr>
  117. </table>
  118. <br/>
  119. <input id="set_number" type="button" value="重新开始抽奖" style="border: 1px solid; border-color: #aaa 000 #000 #aaa;width:45em;height: 3em;background: #fc0;"  onclick="window.location.href=window.location.href;"/>
  120. <p></p>
  121. </div>
  122. <center>
  123. </body>
  124. </html>

3.丑陋截图

我很丑,但是我很温柔~

JavaScript编写简单的抽奖程序