六狼论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博账号登陆

只需一步,快速开始

搜索
查看: 70|回复: 0

用JavaScript写的一个摇号程序(随机数)

[复制链接]

升级  44%

30

主题

30

主题

30

主题

秀才

Rank: 2

积分
116
 楼主| 发表于 2013-2-7 14:55:25 | 显示全部楼层 |阅读模式
<script type="text/javascript"><!----></script>
老弟叫我帮忙他找个摇号程序,号码从001-200。 最近刚看了JS高级编程的随机数这方面的知识。 就自己开始动手做了一个。
代码:

<span style="" />

<script>var theTimer;var iNum=0;//var flag =true;function checkEnter(){if(window.event.keyCode==13){document.getElementById("btnStop").focus();flag=true;if(flag){clearTimeout(theTimer);flag = false;}else{theTimer = setTimeout("checkLoad()", 8);flag=true;}//document.getElementById("btnStop").click();onkeypress="checkEnter();"}}function checkLoad() {iNum=selectFrom(1,200);if(iNum<10){document.getElementById("stext").innerHTML="00"+iNum;}else if(iNum<100){document.getElementById("stext").innerHTML="0"+iNum;}else{document.getElementById("stext").innerHTML=iNum;}theTimer = setTimeout("checkLoad()", 8);}function selectFrom(iFirstValue,iLastValue){var iChoices =  iLastValue-iFirstValue+1;return Math.floor(Math.random()*iChoices+iFirstValue);}function stopPlay() {    clearTimeout(theTimer);}function resetText(){stopPlay();document.getElementById("hisRecord").innerText=document.getElementById("hisRecord").innerText+document.getElementById("stext").innerHTML+",";document.getElementById("stext").innerHTML="000";}</script><body onkeypress="checkEnter();"><table width="100%"><tr height="300"><td align="center"><div id="stext" style="font-size:146px;color:red;bold" align="center">000</div></td></tr><tr height=""><td><div align="center">        <input type="button" value="开始"  style="height:60;width:130"/>    <input id="btnStop" type="button" value="停止"  style="height:60;width:130"/>    <input type="button" value="重置" /></div></td></tr><tr><td align="center"><br />历史记录:<table width="400"><tr ><td><textarea id="hisRecord" style="width:500;height:100"></textarea></td></tr></table></td></tr></table></body></html>
 

发现的问题:<span style="" />

当用事件捕捉到回车键时,执行clearTimeout不能停止。<span style="" />

后来经过试验、分析,发现这是random的缘故。<span style="" />

后来否定的random的原因,原来是聚焦的原因,<span style="" />

没有把焦点从开始按钮上移开。<span style="" />

 

结果:<span style="" />

 
[table=98%][tr][td]<div style="">000
您需要登录后才可以回帖 登录 | 立即注册 新浪微博账号登陆

本版积分规则

快速回复 返回顶部 返回列表