(() => {
  function CountDown() {
    var count = 11,
    tmID,
    hit = Math.floor(Math.random()*10);
    areas = document.querySelectorAll("div"),
    button = document.getElementById("start"),
    infobox = document.getElementById("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);
	console.log(hit+1+"番だよ");
    }
    function stopCountDown() {
        clearTimeout(tmID);
        infobox.innerHTML = "成功!!";
        infobox.setAttribute("class", "success");
    }
    button.addEventListener("click", startCountDown, false);
  }
  document.addEventListener("DOMContentLoaded", CountDown, false);
})();
