首页 > 其他分享 >一个简单的 JS 抽取随机数源代码

一个简单的 JS 抽取随机数源代码

时间:2022-10-21 13:02:41浏览次数:49  
标签:function 抽取 idsArr timer JS 随机数 var 源代码

一个简单的 JS 抽取随机数源代码
自己写了一个 JS 抽取随机数的例子。供自己以后参考,也希望可以方便到大家。
源代码如下:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<mce:script language="javascript" type="text/javascript"><!--
var ids = "1001,1002,1003,1004,1005,1006,1007,1008,1009";//抽取范围,以,为分割;这里是接口:可以使用 EL 获得后台传入数据
var idsArr = ids.split(",");
var num = idsArr.length - 1;//抽取数量
var timer;//定义定时器

function change(){
document.getElementById("expertId").value = idsArr[getEnd(0,num)];//抽取值快速变换
}
function startExt(){
clearInterval(timer); //取消定时器
timer = setInterval('change()',10);//10(毫秒)为变换间隔,越小变换的越快
}
function endExt(){
clearInterval(timer);//取消定时器
}
function getEnd(min,max){
return parseInt(Math.random()*(max-min+1));//随机抽取
}

// --></mce:script>
</head>
<body>
<table>
<tr>
<td align="RIGHT" colspan="8">
<input type="button" value="开始抽取" οnclick="startExt();" name="Button4">
<input type="text" id="expertId" value="" name="expertId" style="width:150px;height:70px;color:red;font-size:70px;border:0px; background:background-color;border-left:none;border-right:none;border-top:none;border-bottom:none;text-align:right;" readonly>
<input type="button" value="停止抽取" οnclick="endExt();" name="Button5">
</td>
</tr>
</table>
</body>
</html>

标签:function,抽取,idsArr,timer,JS,随机数,var,源代码
From: https://blog.51cto.com/defonds/5782413

相关文章