function CountDown() { var count = 30, tmID, infobox = document.getElementById("start"), hit = Math.floor(Math.random()*10+1), areas = document.querySelectorAll("div"); function countDown() { if (--count == "0") { infobox.innerHTML = "おわり"; for (let i of areas) { if (i.textContent == hit) { i.removeEventListener("click", stopCountDown, false); } } } else { infobox.innerHTML = count + "秒前"; tmID = setTimeout(countDown, 1000); } } function startCountDown() { tmID = setTimeout(countDown, 1000); infobox.removeEventListener("click", startCountDown, false); for (let i of areas) { if (i.textContent == hit) { i.addEventListener("click", stopCountDown, false); } } } function stopCountDown() { clearTimeout(tmID); infobox.innerHTML = "あたり"; areas.removeEventListener("click", stopCountDown, false); } infobox.addEventListener("click", startCountDown, false); } document.addEventListener("DOMContentLoaded", CountDown, false);