随机点名器:
<!DOCTYPE html>
<html lang="en">
<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>Document</title>
<style>
*{
margin:0;
padding:0;
}
#main{
margin:100px;
background:rgba(245, 197, 224, 0.5);
width:520px;
padding:50px;
box-shadow:0 2px 12px #666666;
}
#box{
margin:50px;
padding:20px;
width: 250px;
font-size:50px;
}
#btn{
color:red;
width:110px;
font-size:20px;
border-color: gainsboro;
font-weight:bold;
background-color:rgba(225, 247, 225, 0.4);
border-radius:8px 8px 8px 8px;
box-shadow:0 2px 12px #797878;
cursor:pointer;
padding:5px;
}
</style>
</head>
<body>
<center>
<div id="main">
<div id="box">点击开始</div>
<button id="btn" onclick="doit()">start<button>
</div>
</center>
<script>
//准备数据 用数组保存学生姓名
var namelist=["Linda","Kate","Alice","Mike","Vivian","Abby","John","Tim","Jick","Chirs","Lily"];
var btNode = document.getElementById('btn');
var boxNode = document.getElementById('box');
var timer;
var flag =1;
//点击开始按钮
function doit(){
if(flag){
start();
btNode.innerText="stop";
flag = 0;
}else{
stop();
btNode.innerText="start";
flag = 1;
}
}
//点击开始后开始做的事 设置定时任务随机抽取学生
function start(){
timer = setInterval(function(){
var num = random(0,namelist.length);
//显示到页面
boxNode.innerText = namelist[num];
},100)
}
//点击暂停 清除定时器
function stop (){
clearInterval(timer);
}
// 产生随机数 用于数据数组的下标使用
function random(a,b){
return parseInt(Math.random() * (b - a) + a);
}
</script>
</body>
</html>
数组去重:
<!DOCTYPE html>
<html lang="en">
<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>Document</title>
</head>
<body>
<script>
var sz=new Array(2,34,2,5,6,9,8,4,34,67,6)
var newarry=[]
for(i=0;i<sz.length;i++){
for(j=i+1;j<=sz.length;j++){
if(sz[i]==sz[j]){
i++
j=i
}
}
newarry.push(sz[i])
}
console.log(newarry)
</script>
</body>
</html>