(() => {
  function CountDown() {
    var count = 11,
	tmID,
	hit = Math.floor(Math.random()*20),
	areas = document.querySelectorAll("div"),
	button = document.querySelector("#start"),
	infobox = document.querySelector("#result");
    function countDown() {
        if (--count == 0) {
            infobox.innerHTML = "ゲームオーバー";
            infobox.setAttribute("class", "failure")
	    areas[hit].removeEventListener("click", stopCountDown, false);
        } else {
            infobox.innerHTML = count + "秒前";
            tmID = setTimeout(countDown, 1000);
        }
    }
    function startCountDown() {
        tmID = setTimeout(countDown, 1000);
        button.removeEventListener("click", startCountDown, false);
	areas[hit].addEventListener("click", stopCountDown, false);
    }
    function stopCountDown() {
        clearTimeout(tmID);
        infobox.innerHTML = "ゲームクリア!";
        infobox.setAttribute("class", "success");
    }
    button.addEventListener("click", startCountDown, false);
  }
  document.addEventListener("DOMContentLoaded", CountDown, false);
})();
