Answer to Question #306435 in HTML/JavaScript Web Application for swathi

Question #306435

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

Achieve the given functionality using JS.

The timer should be initiated at 0.

  • When the HTML button element with the id
  • startBtn is clicked, the timer should be started.
  • When the HTML button element with the id
  • stopBtn is clicked, the timer should be stopped.
  • When the HTML button element with the id
  • And do not include onclick events in the html button elements.
  • we need to access those dynamically
  • startBtn is clicked after the HTML button element with id stopBtn clicked, the timer should be resumed.
  • Timer should reset to 00 minutes, 00 seconds after one hour.

Note

This is a one hour timer. The maximum time is one hour.




1
Expert's answer
2022-03-05T11:46:45-0500
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <style>
        @import url('https://fonts.googleapis.com/css2?family=Roboto&display=swap');
        * {
            font-family: 'Roboto', sans-serif;
        }
        .wrapper {
            width: 250px;
            display: flex;
            flex-direction: column;
            align-items: center;
        }
        .timer {
            font-size: 50px;
            margin-bottom: 10px;
        }
        button {
            height: 40px;
            width: 100px;
            border-radius: 5px;
            border: none;
            outline: none;
            cursor: pointer;
            color: white;
            font-size: 20px;
        }
        #startBtn { background: #15ac29; }
        #startBtn:hover { background: #128821; }
        #stopBtn { background: #b31515; }
        #stopBtn:hover { background: #8a1010; }
    </style>
    <div class="wrapper">
        <div class="timer">
            <span id="minutes">00</span>:<span id="seconds">00</span>
        </div>
        <div class="btns">
            <button id="startBtn">Start</button>
            <button id="stopBtn">Stop</button>
        </div>
    </div>
    <script>
        const startBtn = document.querySelector('#startBtn'),
              stopBtn = document.querySelector('#stopBtn'),
              minutesHtml = document.querySelector('#minutes'),
              secondsHtml = document.querySelector('#seconds')

        let minutes = 0,
            seconds = 0,
            timer;

        function changeTime() {
            if (++seconds === 60) {
                seconds = 0
                if (++minutes === 60) {
                    minutes = 0;
                    seconds = 0
                }
            }
            minutesHtml.textContent = minutes < 10 ? `0${minutes}` : minutes
            secondsHtml.textContent = seconds < 10 ? `0${seconds}` : seconds
        }

        startBtn.addEventListener('click', () => {
            timer = setInterval(changeTime, 1000)
        })

        stopBtn.addEventListener('click', () => {
            clearInterval(timer)
        })

    </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