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>
運行: