var count = 3, tmID, infobox = document.getElementById("countdown"), rand, clickbutton, stop = false; var endcool = ["お","め","で","と","う","ご","ざ","い","ま","す"], endcoolcount = 0; function countDown() { if(!stop){ if (--count == 0) { infobox.innerHTML = "ぼかーん"; clickbutton.removeEventListener("click", stopf, false); } else { infobox.innerHTML = count + "秒前"; tmID = setTimeout(countDown, 1000); } } } function stopf(){ stop = true; let x = document.querySelectorAll(".button"); console.log(x); for(let i of x){ console.log(i); let a = i.querySelector("p"); a.innerText = endcool[endcoolcount]; endcoolcount++; } clickbutton.removeEventListener("click", stopf, false); } function startCountDown() { tmID = setTimeout(countDown, 1000); infobox.removeEventListener("click", startCountDown, false); rand = Math.floor(Math.random() * 10) + 1; clickbutton = document.getElementById(String(rand)); console.log(clickbutton); clickbutton.addEventListener("click", stopf, false); infobox.innerHTML = count + "秒前"; } infobox.addEventListener("click", startCountDown, false);