Skip to content

Instantly share code, notes, and snippets.

@SuibianP
Last active December 14, 2020 18:31
Show Gist options
  • Save SuibianP/ffdfd0eac8a2ba18231e0643d20a186c to your computer and use it in GitHub Desktop.
Save SuibianP/ffdfd0eac8a2ba18231e0643d20a186c to your computer and use it in GitHub Desktop.
随机连续抽取不重复数组元素的简易HTML静态网页
<!DOCTYPE html>
<!--
本HTML文档由胡佳伦(隋卞)编写。
脚本部分在GNU Affero通用公共许可协议版本3下公开,
其余部分在知识共享署名—相同方式共享4.0公共许可协议国际版下公开。
This HTML document is authored by Hu Jialun (SuibianP).
Scripts are licensed under GNU Affero General Public License version 3,
while other contents are distributed under Creative Commons Attribution-ShareAlike 4.0 International Public License.
-->
<html lang="zh-cmn-Hans-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="author" content="胡佳伦 (Hu Jialun)">
<title>宿舍随机抽取</title>
</head>
<!-- 堆不必要的属性不是好习惯,但是毕竟我不是很会HTML所以( -->
<body onclick=raffle() onkeydown=raffle() style="display:flex;width:100vw;height:100vh;min-height:100vh;min-width:100vw;max-height:100vh;max-width:100vw;margin:0;padding:0;align-items:center;justify-content:center">
<script type="text/javascript">
// 一般来说你只需要改这些变量
const roomNumbers = ["001", "002", "003", "1234"]; // 所有的寝室号
const displayFreq = 20; // 多少毫秒晃一个新的号码
// 其他的部分大概不需要动
let randomizedArray = [];
let isOn = false;
let interval = null;
function raffle() {
isOn = !isOn;
if (isOn) {
interval = setInterval(function () {
document.getElementById("room").innerHTML = roomNumbers[Math.floor(Math.random() * roomNumbers.length)];
}, displayFreq);
} else {
clearInterval(interval);
if (randomizedArray.length == 0) {
randomizedArray = roomNumbers.slice();
for (let i = randomizedArray.length - 1; i > 0; i -= 1) {
const j = Math.floor(Math.random() * (i + 1));
[randomizedArray[i], randomizedArray[j]] = [randomizedArray[j], randomizedArray[i]];
}
}
document.getElementById("room").innerHTML = randomizedArray.pop();
}
}
</script>
<p id="room" title="抽取的宿舍" style="user-select:none;text-align:center;font-size:40vh;"><strong>点击或按任意键开始</strong></p>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment