Answer to Question #309252 in HTML/JavaScript Web Application for king

Question #309252

Hourly Stop Watch

The goal of this coding exam is to quickly get you off the ground with the clearInterval and setInterval.

Refer to the below image.

https://assets.ccbp.in/frontend/content/react-js/hourly-stop-watch-op.gif

HTML

<!DOCTYPE html>

<html>

<head></head>

<body>

  <div class="text-center">

    <p class="timer">

      <span id="minutes">00</span>:<span id="seconds">00</span>

    </p>

    <button class="bg-start-button button" id="startBtn">

      Start

    </button>

    <button class="bg-stop-button button" id="stopBtn">

      Stop

    </button>

  </div>

</body>

</html>






1
Expert's answer
2022-03-10T18:07:24-0500
<!DOCTYPE html>
<html lang="en">
<head></head>
<body>
    <div class="text-center">
        <p class="timer">
            <span id="minutes">00</span>:<span id="seconds">00</span>
        </p>
        <button class="bg-start-button button" id="startBtn">Start</button>
        <button class="bg-stop-button button" id="stopBtn">Stop</button>
    </div>


    <style>
        .text-center {
            width: 100%;
            text-align: center;
        }
        p {
            text-align: center;
            font-size: 24px;
            letter-spacing: 2px;
        }
        .button {
            padding: 5px 15px;
            color: #fff;
            outline: none;
            border: none;
            border-radius: 3px;
            cursor: pointer;
        }
        .bg-start-button {
            background-color: green;
        }
        .bg-stop-button {
            background-color: red;
        }
    </style>


    <script>
        let min = document.getElementById('minutes');
        let sec = document.getElementById('seconds');
        let startBtn = document.getElementById('startBtn');
        let stopBtn = document.getElementById('stopBtn');
        let counter = 0;
        let play = null;


        startBtn.onclick = () => {
            if (play !== null) {
                clearInterval(play);
            }


            play = setInterval(() => {
                counter++;


                if (counter < 60) {
                sec.innerText = counter < 10 ? '0' + counter : counter;
                } else {
                    let num = Math.floor(counter / 60);
                    min.innerText = num < 10 ? '0' + num : num;
                    sec.innerText = counter % 60 < 10 ? '0' + counter % 60 : counter % 60;
                }
            }, 1000)
        }


        stopBtn.onclick = () => {
            clearInterval(play);
        }
    </script>
</body>
</html>

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS