Newer
Older
2024-C1231033-fuma / bomb.js
@c123103 c123103 on 8 Nov 1 KB ch:js
#!/usr/bin/env js

document.addEventListener("DOMContentLoaded", () => {
    var startButton = document.getElementById("button");
    var bombDivs = document.querySelectorAll(".bomb div");
    var isClickable = false;
    var countdownTimer;
    var winnerIndex;

    startButton.addEventListener("click", () => {
       
        winnerIndex = Math.floor(Math.random() * bombDivs.length);
        isClickable = true;

        var countdown = 3;
        document.body.insertAdjacentHTML("beforeend", `<p id="status">${countdown}秒前...</p>`);

        countdownTimer = setInterval(() => {
            countdown--;
            var status = document.getElementById("status");
            if (countdown > 0) {
                status.textContent = countdown + "秒前...";
            } else {
                clearInterval(countdownTimer);
                isClickable = false;
                status.textContent = "失敗!";
            }
        }, 1000);
    });
    
bombDivs.forEach((div, index) => {
    div.addEventListener("click", () => {
        if (isClickable && index === winnerIndex) {
            clearInterval(countdownTimer);
            document.getElementById("status").textContent = "当たり!";
            div.style.color = "red"; // 当たりの色を赤に変更
            isClickable = false;
        }
    });
});

});