2008-07-17
用JavaScript写的一个摇号程序(随机数)
关键字: 用javascript写的一个摇号程序(随机数)
老弟叫我帮忙他找个摇号程序,号码从001-200。 最近刚看了JS高级编程的随机数这方面的知识。 就自己开始动手做了一个。
代码:
<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="开始" onClick="checkLoad()" style="height:60;width:130"/>
<input id="btnStop" type="button" value="停止" onClick="stopPlay();" style="height:60;width:130"/>
<input type="button" value="重置" onClick="resetText();"/>
</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>
发现的问题:
当用事件捕捉到回车键时,执行clearTimeout不能停止。
后来经过试验、分析,发现这是random的缘故。
后来否定的random的原因,原来是聚焦的原因,
没有把焦点从开始按钮上移开。
结果:
|
000
|
|
|
|
|
历史记录: |
- 15:04
- 浏览 (95)
- 评论 (0)
- 分类: javaScript
- 相关推荐
发表评论
- 浏览: 27591 次
- 性别:

- 来自: 杭州

- 详细资料
搜索本博客
最近加入圈子
最新评论
-
就这样,打造一个实用的Ub ...
不错的东西,前段时间装了没用过
-- by darkjune -
Liferay和LDAP
想要更多了解 Liferay ,大家可以联系大连Peter,可以从他那得到足够帮 ...
-- by 010lead -
加载Spring的 Web.xml配置
引用 [img][/img][url][/url][flash=200,2 ...
-- by xingzhejh -
就这样,打造一个实用的Ub ...
留言占个位先
-- by tcrct -
tomcat集群和负载均衡的实 ...
好贴,支持
-- by j2eeli






评论排行榜