随机点名小程序
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<div style="margin: auto;width: 500px;" >
<input type="file" id="file" accept=".txt"/>
<input type="button" name="11111" id="but" value="开始" onclick="startTimer()">
<input type="button" name="11111" id="but" value="暂停" onclick="stopTimer()">
</div>
<div style="margin-top: 30px;">
<p style="width: 500px; height: 60px;margin: auto; color: red;font-size: 28px;border: solid 1px sienna;" id="p1"></p>
</div>
<div>
使用步骤:<br/>
<ol>
<li>将Excel表格中的名单全选复制粘贴到txt文本文件中,然后保存到自己知道的位置</li>
<li>点击选择文件,选择刚才保存的txt文件</li>
<li>点击开始,即可滚动,点击暂停,即可暂停滚动</li>
</ol>
</div>
<script>
var datas = new Array;
var str=[];
//读取本地的TXT文件
document.getElementById("file").onchange = function(){
var file = this.files[0]; //获取文件
var reader = new FileReader();
var i = 0;
reader.readAsText(file, "gb2312");
reader.onload = function() {
datas[0] = reader.result.split('\n');
}
console.log(datas);
str = datas;
}
// 输出信息
function print(){
var min = 0;
var max = str[0].length;
var n = random(min,max)
document.getElementById("p1").innerHTML=str[0][n];
}
// 计时事件
var myVar;
//开始计时
function startTimer(){
if(str.length != 0){
myVar =setInterval(function(){print()},10);
}else{
alert("请选择txt文件")
}
}
//停止计时
function stopTimer(){
clearInterval(myVar);
}
// 随机数
function random(min, max) {
return Math.floor(Math.random() * (max - min)) + min;
}
</script>
</body>
</html>