这期内容当中小编将会给大家带来有关使用原生JavaScript怎么编写一个随机点名功能,文章内容丰富且以专业的角度为大家分析和叙述,阅读完这篇文章希望大家可以有所收获。
代码如下:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>随机点名</title>
<style>
.box{
width: 800px;
margin: 200px auto;
text-align: center;
}
.box h3{
font-size: 55px;
font-weight: 800;
}
.box input{
outline: none;
border: none;
background: blue;
color: white;
width: 100px;
height: 50px;
font-size: 15px;
font-weight: 600;
}
</style>
</head>
<body>
<div class="box">
<h3 id="res">请点击下面的按钮,开始抽奖!</h3>
<input type="button" value="点击开始" id="btn">
</div>
<script>
var btn= document.getElementById("btn");
var res= document.getElementById("res");
var student=["张三","李四","王二","麻子","小明","小王","小胡","小虎","狗子","多银币","贡子哥","刘青松","瞎子","亚索"]
var flag=true;
var timer=null;
//
btn.onclick=function(){
if(flag){
timer= setInterval(function(){
var index= getRandom(student.length-1,0) ;
res.innerHTML=student[index];
},10);
btn.value="点击结束";
flag=false;
}else{
clearInterval(timer);
btn.value="点击开始";
flag=true;
}
}
function getRandom(max,min){
return Math.round(Math.random()*(max-min)+min);
}
</script>
</body>
</html>
上述就是小编为大家分享的使用原生JavaScript怎么编写一个随机点名功能了,如果刚好有类似的疑惑,不妨参照上述分析进行理解。如果想知道更多相关知识,欢迎关注亿速云行业资讯频道。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。