本文实例为大家分享了js随机点名器的具体代码,供大家参考,具体内容如下
<html lang="zh"> <head> <Meta charset="UTF-8"> <Meta name="viewport" content="width=device-width,initial-scale=1.0"> <Meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>随机抽签</title> </head> <body> <center> <div id="d" style="background: #7A7A7A;width: 250px;"><!-- 设置背景框颜色和长度 --> <h1 id = 'name'></h1> <!-- 用于显示随机的人名 --> </div> <div> <button id = 'start' type="button">开始</button> </div> <div> <button id = 'temp' type="button">暂停</button> </div> </center> </body> </html> <script type="text/javascript"> window.onload=function(){ var names = ['name','name','name']; //人名数组 var clors = ['#EE0000','#CAE1FF','#008B8B','#CDC9C9','#F0F8FF'];//颜色数组 var name = document.getElementById('name'); //获取id为name的元素 var d = document.getElementById('d'); var temp = document.getElementById('temp'); var startR = -1; //记录计时器的序号 var start = document.getElementById('start'); function Random(){ //产生随机数,并更改h1中的内容与颜色 var nNum = Math.floor(Math.random()*5); //这里的5为你当前人名的数量 var cNum = Math.floor(Math.random()*5); name.innerHTML = names[nNum]; d.style.color = clors[cNum]; } start.onclick = function(){ //单击开始方法 if(startR == -1) //不加限制,多次点击开始会有多个计时器启动 startR = setInterval(Random,25);//设置计时器,每0.025秒执行一次 } temp.onclick = function(){ //单击暂停方法 clearInterval(startR); startR = -1; } } </script>
具体展示