<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<style>
div {
width: 200px;
height: 60px;
border: 5px solid hotpink;
text-align: center;
line-height: 60px;
}
button {
width: 210px;
height: 30px;
text-align: center;
line-height: 30px;
}
</style>
<body>
<div>开始点名!</div>
<button>随机点名</button>
<script>
let box = document.querySelector('div');
let btn = document.querySelector('button');
function getRandom(min,max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
let flag=1;
let arr = ['张三','李四','王五','刘二'];
let ste;
btn.addEventListener('click', function() {
if(flag) {
btn.innerHTML = '暂停';
flag = 0;
ste = setInterval(function () {
box.innerHTML = arr[getRandom(0, 3)];
}, 100);
}
else
{
btn.innerHTML = '开始点名!';
flag=1;
clearInterval(ste);
}
});
</script>
</body>
</html>
随机点名效果
最新推荐文章于 2023-06-09 16:49:45 发布