Newer
Older
ryuei / random.js
@Ryuei Ryuei on 14 Nov 2022 1 KB fix code
function main() {
    var count = 4,tmID,button = document.getElementById("start"),atari = document.querySelectorAll("div"),finish = document.getElementById("info"),random = Math.floor(Math.random()*10+1);
    button.addEventListener("click", startCountDown, false);
    document.getElementById("reload").addEventListener("click",function(){window.location.reload();})
    console.log(random);
    
    function CountDown(){
        if (--count == 0) {
            finish.innerHTML = "時間切れ";
            for(let i of atari){
                if(i.textContent == random){
                    i.removeEventListener("click", stopCountDown, false);
                }
            }
        } else {
            finish.innerHTML = count + "秒前";
            tmID = setTimeout(CountDown, 1000);/*1000ms=1秒*/ 
        }
    }

    function stopCountDown() {
        if (count >= 0) {
            clearTimeout(tmID);
            button.innerHTML = "爆発阻止成功です!";
        }
        finish.innerHTML = "助かった〜〜";
        atari.innerHTML = "みっけ!";
    }

    function startCountDown() {
        tmID = setTimeout(CountDown, 1000);/*1000ms=1秒*/ 
        button.removeEventListener("click", startCountDown, false);
        for(let i of atari){
            if(i.textContent == random){
                i.addEventListener("click", stopCountDown, false);
            }
        }
    }
};
document.addEventListener("DOMContentLoaded", main, false);