js实现随机点名小功能
本文实例为大家分享了js实现随机点名功能的具体代码,供大家参考,具体内容如下
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<div id="name1"></div>
<button id="btn">stop</button>
</body>
<script type="text/javascript">
var flag = true;
var str = "张三,李四,王五,马六";
var arr = str.split(",");
var interId = setInterval("randName()",100);
function randName(){
var rand = Math.floor(Math.random() * arr.length);
name1.innerHTML = arr[rand];
}
btn.onclick = function (){
if (flag) {
btn.innerHTML = "start";
clearInterval(interId);
flag = false;
} else{
btn.innerHTML = "stop";
interId = setInterval("randName()",100);
flag = true;
}
}
</script>
</html>
以上是 js实现随机点名小功能 的全部内容, 来源链接: utcz.com/z/342521.html