mirror of
https://github.com/zyllian/pipefall.git
synced 2025-01-18 03:32:55 -08:00
add random chance of playing
This commit is contained in:
parent
d246b4b0ac
commit
02b4b28959
2 changed files with 31 additions and 0 deletions
|
@ -15,6 +15,13 @@
|
|||
<img class="pipe" src="/pipe.jpg" alt="metal pipe from metal pipe fallout sound effect videos">
|
||||
</p>
|
||||
<p><button id="play-now">play now</button></p>
|
||||
<p>
|
||||
<input type="checkbox" id="play-at-random">
|
||||
<label for="play-at-random">play at random</label>
|
||||
</p>
|
||||
<p>
|
||||
<label>one in <input type="number" min="0" value="100" id="random-chance"> chance of playing every minute</label>
|
||||
</p>
|
||||
</body>
|
||||
|
||||
</html>
|
||||
|
|
24
index.js
24
index.js
|
@ -3,6 +3,10 @@
|
|||
|
||||
const pipefall = document.getElementById("pipefall");
|
||||
const now = document.getElementById("play-now");
|
||||
const playAtRandom = document.getElementById("play-at-random");
|
||||
const randomChance = document.getElementById("random-chance");
|
||||
|
||||
playAtRandom.checked = false;
|
||||
|
||||
const play = () => {
|
||||
console.log("pipefall!");
|
||||
|
@ -14,4 +18,24 @@
|
|||
for (let pipe of document.querySelectorAll(".pipe")) {
|
||||
pipe.addEventListener("click", play);
|
||||
}
|
||||
|
||||
function random(max) {
|
||||
return Math.floor(Math.random() * max);
|
||||
}
|
||||
|
||||
let timeSincePlay = 0;
|
||||
setInterval(() => {
|
||||
if (playAtRandom.checked) {
|
||||
timeSincePlay += 1;
|
||||
if (timeSincePlay >= 60) {
|
||||
const v = random(randomChance.value);
|
||||
if (v === 0) {
|
||||
play();
|
||||
timeSincePlay = 0;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
timeSincePlay = 0;
|
||||
}
|
||||
}, 1000);
|
||||
})();
|
||||
|
|
Loading…
Add table
Reference in a new issue