Newer
Older
2025-SatoHiromasa_gd / kadai2.js
(() => {
  function Quiz(){
    var count = 10, tmID, infobox = document.getElementById("time");
    function countDown() {
      if (--count == 0) {
        infobox.innerHTML = "失敗...";
	if (quizTarget) {
          quizTarget.removeEventListener("click", stopCountDown);
        }
      } else {
        infobox.innerHTML = "残り" + count + "秒";
        tmID = setTimeout(countDown, 1000);
      }
    }
    function startCountDown() {
      tmID = setTimeout(countDown, 1000);
      document.getElementById("start").removeEventListener("click", startCountDown, false);
    }
    function stopCountDown() {
      clearTimeout(tmID);
      infobox.innerHTML = "成功!";
      if (quizTarget) {
        quizTarget.removeEventListener("click", stopCountDown);
      }
      document.getElementById("start").removeEventListener("click", stopCountDown, false);
    }
    
    function quiz() {
        var ans_no  = Math.floor(Math.random() * 10) + 1;
	quizTarget = document.getElementById(ans_no);
        quizTarget.addEventListener("click", stopCountDown);
    }
    document.getElementById("start").addEventListener("click", startCountDown, false);
    document.getElementById("start").addEventListener("click", quiz);
  };
  document.addEventListener("DOMContentLoaded", Quiz, false);
})();