Newer
Older
2023-KAito / game.js
(() => {
    var timer = null, count = 3, msgArea = null;
    var answer;
    function countdown() {
	count--;
	if (count==0) {
	document.body.style.background = "#696969 ";
	    msgArea.innerText = `時間切れ~ 押すのは${answer+1}だよ`;
	    msgArea.style.fontSize = "300%";
	    msgArea.style.textAlign = "center";
	} 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 = "#00ffff";
	    msgArea.innerText = "おめでとう🎉";
	    msgArea.style.fontSize = "300%";
	    msgArea.style.textAlign = "center";
	} else {clearTimeout(timer);
	    document.body.style.background = "#696969 ";
	    msgArea.innerText = `失敗 押すのは${answer+1}だよ`;
	    msgArea.style.fontSize = "300%";
	    msgArea.style.textAlign = "center";
	}
    }
    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);
	    msgArea.innerText = `押すのは${answer+1}だよ`;
	    msgArea.style.fontSize = "250%";
	     msgArea.style.textAlign = "center";
	}
	timer = setTimeout(countdown, 1000);
    };
    document.addEventListener("DOMContentLoaded", init);
})();