首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > JavaScript >

JS编写的抽奖程序,该怎么解决

2012-03-09 
JS编写的抽奖程序公司要开年会了,我这里要做个抽奖程序,我一直没怎么接触过JS,憋了半天,憋出个这样的程序:

JS编写的抽奖程序
公司要开年会了,我这里要做个抽奖程序,我一直没怎么接触过JS,憋了半天,憋出个这样的程序:
<html>
<head>
<title> Untitled   Document </title>
<meta   http-equiv= "Content-Type "   content= "text/html;   charset=gb2312 ">
<style   type= "text/css ">
<!--
.unnamed1   {
height:   150px;
width:   200px;
border:   0px   none;
font-size:   100px;
font-weight:   bolder;
color:   #FF0000;
text-align:   center;
}
-->
</style>
</head>
<script   language= "javascript ">
var   con= 'start ';

function   showme(){
if   (con== 'start ')
    {document.form.textfield.value= '0 '   +   Math.round(Math.random()*500);
      timeid=setTimeout( 'showme() ',50);}
}

function   startshowme(){
con= 'start ';
showme();
}


function   stopshowme(){
con= 'stop ';
}
</script>
<body>
<p> &nbsp; </p>
<p> <br>
</p>
<table   width= "100% "     border= "0 "   align= "center "   cellpadding= "0 "   cellspacing= "0 ">
    <tr>
        <td   align= "center "> <form   name= "form "   method= "post "   action= " ">
            <input   name= "textfield "   type= "text "   class= "unnamed1 "     onFocus= "this.blur() "   size= "30 ">
        </form> </td>
    </tr>
    <tr>
        <td   align= "center "> <input   type= "button "   name= "Button "   value= "开始 "   onClick= "startshowme() ">   
        <input   type= "button "   name= "Button "   value= "终止 "   onClick= "stopshowme() "> </td>
    </tr>
</table>
</body>
</html>

现在有这样的问题想请教各位
1.抽过的号码不能重复
2.数字一直保持   在3位,比如001,020,140这样的
由于比较急,所以希望各位能给出代码谢谢了

[解决办法]
<html>
<head>
<title> Untitled Document </title>
<meta http-equiv= "Content-Type " content= "text/html; charset=gb2312 ">
<style type= "text/css ">
<!--
.unnamed1 {
height: 150px;
width: 200px;
border: 0px none;
font-size: 100px;
font-weight: bolder;
color: #FF0000;
text-align: center;
}
-->
</style>
</head>
<script language= "javascript ">
var con= 'start ';
var value = 0;
var valueArray = new Array();

function showme() {

if (con== 'start ') {

do {
value = Math.round(Math.random()*4 + 1);

if ( value < 10 )
value = "00 " + value;

if ( value < 100 )
value = "0 " + value;
}
while ( valueArray[value] )



document.form.textfield.value = value;

timeid=setTimeout( 'showme() ',50);

}
else {

valueArray[value] = value;

}

}

function startshowme(){
con= 'start ';
showme();
}


function stopshowme(){
con= 'stop ';
}
</script>
<body>
<p> &nbsp; </p>
<p> <br>
</p>
<table width= "100% " border= "0 " align= "center " cellpadding= "0 " cellspacing= "0 ">
<tr>
<td align= "center "> <form name= "form " method= "post " action= " ">
<input name= "textfield " type= "text " class= "unnamed1 " onFocus= "this.blur() " size= "30 ">
</form> </td>
</tr>
<tr>
<td align= "center "> <input type= "button " name= "Button " value= "开始 " onClick= "startshowme() ">   
<input type= "button " name= "Button " value= "终止 " onClick= "stopshowme() "> </td>
</tr>
</table>
</body>
</html>
[解决办法]
<html>
<head>
<title> Untitled Document </title>
<meta http-equiv= "Content-Type " content= "text/html; charset=gb2312 ">
<style type= "text/css ">
<!--
.unnamed1 {
height: 150px;
width: 200px;
border: 0px none;
font-size: 100px;
font-weight: bolder;
color: #FF0000;
text-align: center;
}
-->
</style>
</head>
<script language= "javascript ">
var con= 'start ';

function showme(){
if (con== 'start ')
{
var value=Math.round(Math.random()*500);
var zero= " ";
if(value/100 <1){
zero+= "0 ";
}
if(value/10 <1){
zero+= "0 ";
}
document.form.textfield.value=zero + Math.round(Math.random()*500);
timeid=setTimeout( 'showme() ',50);}
}

function startshowme(){
con= 'start ';
showme();
}


function stopshowme(){
con= 'stop ';
}
</script>
<body>
<p> &nbsp; </p>
<p> <br>
</p>
<table width= "100% " border= "0 " align= "center " cellpadding= "0 " cellspacing= "0 ">
<tr>
<td align= "center "> <form name= "form " method= "post " action= " ">
<input name= "textfield " type= "text " class= "unnamed1 " onFocus= "this.blur() " size= "30 ">
</form> </td>
</tr>
<tr>
<td align= "center "> <input type= "button " name= "Button " value= "开始 " onClick= "startshowme() ">   
<input type= "button " name= "Button " value= "终止 " onClick= "stopshowme() "> </td>
</tr>
</table>
</body>
</html>
------解决方案--------------------


try
----------------------------
<style type= "text/css ">
<!--
#d1{
height: 150px;
width: 200px;
border: 0px none;
font-size: 100px;
font-weight: bolder;
color: #FF0000;
text-align: center;
}
-->
</style>
</head>
<script language= "javascript ">

var intl;

function loop(){
var str = '00 ' + parseInt(Math.random()*500);
str = str.substring(str.length-3,str.length);
d1.innerText = str;
return str;
}

function Start(){
intl = window.setInterval( "loop(); ",10);
}

function stop(){
if(d2.innerHTML.indexOf(d1.innerText) <0){
d2.innerHTML += d1.innerText + " <br> ";
}
else{
while(d2.innerHTML.indexOf(loop1()) <0){
d2.innerHTML += d1.innerText + " <br> ";
}
}
window.clearInterval(intl);
}

</script>

<div id= "d1 "> </div>
<input type= "button " name= "Button " value= "开始 " onClick= "Start() ">   
<input type= "button " name= "Button " value= "终止 " onClick= "stop() ">
结果:
<div id= "d2 "> </div>

热点排行
Bad Request.