JavaScript写的抽奖程序
JavaScript写的抽奖程序
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 TRANSITIONAL//EN">
<html>
<head>
<title> 抽奖程序 </title>
<script language="javascript">
var timer;
var flag = 0;
//获取随机数
function getRandomNum(){
var f1 = document.getElementById("f1");
var f2 = document.getElementById("f2");
var fv1 = f1.value;
var fv2 = f2.value;
var result = document.getElementById("result");
var rand = 0;
if (!flag && (fv1=="" || fv2=="" || fv1 >= fv2)){
flag = 1;
alert("必须输入开始数字和结束数字,并且开始数不能大于结束数");
return false;
}
fv2 = fv2*1.5;
rand = parseInt(Math.random() *(fv2-fv1+1)+fv1);
if (rand>f1.value && rand<f2.value){
result.value = rand;
}
}
//开始
function setTimer(){
timer = setInterval("getRandomNum();",10);
document.getElementById("start").disabled = true;
document.getElementById("end").disabled = false;
}
//停止
function clearTimer(){
clearInterval(timer);
document.getElementById("start").disabled = false;
document.getElementById("end").disabled = true;
}
</script>
</head>
<body>
<center>
<div> </div>
<div id="main">
<div>
<h1>抽奖程序</h1>
<p>范围:<input id="f1" type="text" size="10" maxlength="15" value="100000" style="border:1px solid #000;" /> - <input id="f2" type="text" size="10" maxlength="15" value="300000" style="border:1px solid #000;" /></p>
<p>结果:<input id="result" type="text" size="10" style="height:40px;width:180px;border:1px solid red;font-size:30;" readonly/></p>
<input id="start" type="button" value="开始" style="border: 1px solid; border-color: #aaa 000 #000 #aaa;width:4em; background: #fc0;" onclick="setTimer()" />
<input id="end" type="button" value="抽奖" style="border: 1px solid; border-color: #aaa 000 #000 #aaa;width:4em; background: #fc0;"onclick="clearTimer()" disabled/>
</div>
</div>
<center>
</body>
</html>
运行截图: