From 02b4b28959ad84ee138ab8db198170e5dad1ff0d Mon Sep 17 00:00:00 2001 From: Zoey Date: Sun, 23 Apr 2023 11:31:02 -0700 Subject: [PATCH] add random chance of playing --- index.html | 7 +++++++ index.js | 24 ++++++++++++++++++++++++ 2 files changed, 31 insertions(+) diff --git a/index.html b/index.html index 63ec94b..17ec28e 100644 --- a/index.html +++ b/index.html @@ -15,6 +15,13 @@ metal pipe from metal pipe fallout sound effect videos

+

+ + +

+

+ +

diff --git a/index.js b/index.js index 91f2445..6fd5d12 100644 --- a/index.js +++ b/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); })();