본문 바로가기
AI웹페이지코딩

스톱워치 HTML 코드

by 홈페이지스 블로그 2023. 11. 5.

스톱워치 HTML코드입니다.

 

<!DOCTYPE html>
<html>
<head>
    <style>
        body {
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
            margin: 0;
        }

        .stopwatch-container {
            width: 60%;
            display: flex;
            flex-direction: column;
            align-items: center;
            text-align: center;
            background-color: #f0f0f0;
            padding: 20px;
            border: 2px solid #333;
            border-radius: 10px;
        }

        #display {
            font-size: 3rem;
            margin: 20px 0;
        }

        #startStopButton {
            padding: 10px 20px;
            font-size: 1rem;
        }

        input[type="number"] {
            padding: 5px;
            font-size: 1rem;
        }
    </style>
</head>
<body>
    <div class="stopwatch-container">
        <h1 id="display">00:00</h1>
        <button id="startStopButton" onclick="startStop()">시작</button>
        <input type="number" id="inputSeconds" placeholder="초 입력">
    </div>
    <audio id="beep" src="beep.mp3"></audio>
    <script>
        let timer;
        let isRunning = false;
        let secondsInput = document.getElementById("inputSeconds");
        let display = document.getElementById("display");
        let startStopButton = document.getElementById("startStopButton");
        let beep = document.getElementById("beep");

        function startStop() {
            if (isRunning) {
                stopTimer();
            } else {
                startTimer();
            }
        }

        function startTimer() {
            let seconds = parseInt(secondsInput.value);
            if (isNaN(seconds) || seconds <= 0) {
                alert("올바른 초를 입력하세요.");
                return;
            }

            isRunning = true;
            startStopButton.innerHTML = "멈춤";

            timer = setInterval(function () {
                seconds--;
                displayTime(seconds);

                if (seconds === 0) {
                    stopTimer();
                    beep.play();
                }
            }, 1000);
        }

        function stopTimer() {
            clearInterval(timer);
            isRunning = false;
            startStopButton.innerHTML = "시작";
        }

        function displayTime(seconds) {
            const minutes = Math.floor(seconds / 60);
            const remainingSeconds = seconds % 60;
            display.innerHTML = `${minutes.toString().padStart(2, "0")}:${remainingSeconds.toString().padStart(2, "0")}`;
        }
    </script>
</body>
</html>

 

이 코드를 실행하면 다음과 같은 화면이 나옵니다.

AI로 코딩한 것입니다.