(() => { var timer = null, count = 5, msgArea = null; var answer; function countdown() { count--; if (count==0) { alert("ぼかあああああああああん"); } else { msgArea.innerText = `爆発まであと ${count}秒` timer = setTimeout(countdown, 1000); } } function stop(e) { console.log(`target = ${e.target}`) if (e.target.innerText == answer+1) { clearTimeout(timer); document.body.style.background = "#e99"; msgArea.innerText = "世界は救われた"; msgArea.style.fontSize = "200%"; msgArea.style.textAlign = "center"; } else { //ハズレの処理 e.target.style.background = "white"; e.target.textContent = `ハズレ\nだよ`; } } function init() { let divs = document.querySelectorAll("div"); msgArea = document.getElementById("message"); if (divs.length == 0) { alert("divがないよ!"); return; } answer = Math.floor(Math.random()*10) console.log(`当たりは${answer+1}だよ`) for (let i of divs) { i.addEventListener("click", stop); } timer = setTimeout(countdown, 1000); }; document.addEventListener("DOMContentLoaded", init); })();