Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Auto Save

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <!DOCTYPE html>
<html>

<head>
    <title>Timer</title>
</head>

<body>
    <div class="container">
        <h1>Timer</h1>
        <div class="ipt">
            <input id="inputh" type="number" placeholder="hour">
            <input id="inputm" type="number" placeholder="minute">
            <input id="inputs" type="number" placeholder="second">
        </div>
        <div class="btn">
            <button id="btn-start" onclick="start_counting()">Start</button>
            <button id="btn-pause" onclick="pause_counting()">Pause</button>
            <button id="btn-stop" onclick="end_counting()">Stop</button>
        </div>
        <p id="currentTime">current time: </p>
    </div>
</body>
              
            
!

CSS

              
                
    .container {
        margin: 0 auto;
        width: 300px;
        height: 300px;
        display: flex;
        flex-direction: column;
        justify-content: center;
        align-items: center;
    }

    .ipt {
        margin: 0 auto;
        display: flex;
        justify-content: center;
        align-items: center;
    }

    input {
        width: 100px;
        height: 50px;
        font-size: 20px;
        text-align: center;
    }

    .btn {
        margin: 10px;
        display: flex;
        justify-content: center;
        align-items: center;
    }

    button {
        margin: 10px 10px;
        width: 50px;
        height: 30px;
        font-size: 10px;
    }

    #currentTime {
        margin: 10px;
        color: green;
    }

              
            
!

JS

              
                
    // initialize button state
    document.getElementById("btn-pause").disabled = true;
    document.getElementById("btn-stop").disabled = true;

    // define global variables
    var timer = null; // store the returned value of timer
    var h = 0; // store the value of hour
    var m = 0; // store the value of minute
    var s = 0; // store the value of second

// restrict the input range of hour, minute and second numbers
var inputh = document.getElementById("inputh");
inputh.addEventListener("input", function() { 
    inputh.value = parseInt(inputh.value||0);
    if (inputh.value > 24) inputh.value = 24;
    if (inputh.value < 0) inputh.value = 0;
});

var inputm = document.getElementById("inputm");
inputm.addEventListener("input", function() {
    inputm.value = parseInt(inputm.value||0);
    if (inputm.value > 59) inputm.value = 59;
    if (inputm.value < 0) inputm.value = 0;
});

var inputs = document.getElementById("inputs");
inputs.addEventListener("input", function() {
    inputs.value = parseInt(inputs.value||0);
    if (inputs.value > 59) inputs.value = 59;
    if (inputs.value < 0) inputs.value = 0;
});

    // define a function
    // start the timer
    function start_counting() {
        // get the time entered or set a default value
        h = +document.getElementById("inputh").value || h;
        m = +document.getElementById("inputm").value || m;
        s = +document.getElementById("inputs").value || s;

        // check for illegal input
        if (
            (h == 0 && m == 0 && s == 0) ||
            (h < 0 || m < 0 || s < 0)
        ) {
            alert("The time entered is illegal!");
            return;
        }

        // start the timer
        timer = setInterval(counting, 1000);
      // optimize the format of hour, minute, and second numbers
h = h.toString();
m = m.toString();
s = s.toString();
if (h.match(/^\d$/)) { // If the hour is a single digit, add 0 in the front
    h = "0" + h;
}
if (m.match(/^\d$/)) { // If the minute is a single digit, add 0 in the front
    m = "0" + m;
}
if (s.match(/^\d$/)) { // If the second is a single digit, add 0 in the front
    s = "0" + s;
}
      
        // change the state of buttons and input fields to prohibit users from re-entering numbers
        document.getElementById("btn-start").disabled = true;
        document.getElementById("btn-pause").disabled = false;
        document.getElementById("btn-stop").disabled = false;
        document.getElementById("inputh").disabled = true;
        document.getElementById("inputm").disabled = true;
        document.getElementById("inputs").disabled = true;
    }

    // pause the timer
    function pause_counting() {
        // change the state of buttons and input fields to allow users to re-enter numbers
        document.getElementById("btn-start").disabled = false;
        document.getElementById("btn-pause").disabled = true;
        document.getElementById("btn-stop").disabled = false;
        document.getElementById("inputh").disabled = false;
        document.getElementById("inputm").disabled = false;
        document.getElementById("inputs").disabled = false;

        // pause the timer
        clearInterval(timer);
    }

    // stop the timer
    function end_counting() {
        // change the state of buttons and input fields to allow users to re-enter numbers
        document.getElementById("btn-start").disabled = false;
        document.getElementById("btn-pause").disabled = true;
        document.getElementById("btn-stop").disabled = true;
        document.getElementById("inputh").disabled = false;
        document.getElementById("inputm").disabled = false;
        document.getElementById("inputs").disabled = false;

        // stop the timer
        clearInterval(timer);

        // reset the time variables
        h = 0;
        m = 0;
        s = 0;
        document.getElementById("currentTime").innerHTML = "Timer stopped";
    }

    // countdown
    function counting() {
        // check if the second is 0
        if (s == 0) {
            // check if the minute is 0 when the second is 0
            if (m == 0) {
                // the entered time has already been checked for legality before starting the timer, so there is no need to check the value of the variable h again here
                h--;
                m = 59;
                s = 59;
            } else {
                // when the minute is not 0, the minute minus 1 and the second becomes 59
                m--;
                s = 59;
            }
        } else {
            // when the second is not 0, the second minus 1
            s--;
        }

        // display current time
        document.getElementById("currentTime").innerHTML = "current time: " + h + " h " + m + " m " + s + " s";
        document.getElementById("inputh").value = h;
        document.getElementById("inputm").value = m;
        document.getElementById("inputs").value = s;

        // check if the second is 0
        if (s == 0) {
            // when the second is 0, check if the minute is 0
            if (m == 0) {
                // when the minute is 0, check if the hour is 0
                if (h == 0) {
                    // when the hour is 0, stop the timer
                    // stop the timer
                    end_counting();
                    // execute popup in the next event loop to prevent it from blocking DOM rendering
                    setTimeout(function () {
                        alert("The time is up!");
                    }, 0);
                    return;
                }
            }
        }
    }


              
            
!
999px

Console