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

<script type="text/javascript"><!----></script>

老弟叫我帮忙他找个摇号程序,号码从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
                  

历史记录:
</td> </tr> </table> </td> </tr> </table> </div>
分享到:
评论

你可能感兴趣的:(JavaScript,html,编程)