Search code examples
javascripttimerelementsetintervalcountdowntimer

Updating the button element as a countdown timer through javascript


I want to create a countdown timer which looks like an fps counter for webpage... after hours of time spent i m not able to find out what is wrong.....help

<script>
    var myvar = setInterval(function () { startTimer() }, 1000);

    function startTimer() {
        var presentTime = 17 + ":" + 00;
        var timeArray = presentTime.split(/[:]+/);
        var m = timeArray[0];
        var s = checkSecond((timeArray[1] - 1));
        if (s == 59) {
            m = m - 1
        }
        //if(m<0){alert('timer completed')}

        var button2 = document.createElement("Button2");
        var interval = m + s;
        button2.innerHTML = Math.round(interval);

        button2.style = "top:0; left:0rem; height:10% ;color: black; background-color: #ffffff;position:fixed;padding:20px;font-size:large;font-weight: bold;";

        setTimeout(startTimer, 1000);
        document.body.appendChild(button2);
    }

    function checkSecond(sec) {
        if (sec < 10 && sec >= 0) {
            sec = "0" + sec
        }; // add zero in front of numbers < 10
        if (sec < 0) {
            sec = "59"
        };
        return sec;
    }
</script>

Solution

  • I can find three errors that hinder your code from performing correctly.

    Multiple timers

    First off since you invoke both a setInterval in outer scope, and then a setTimeout after each performed iteration, you will end up getting many unwanted timer instances that will do some crazy counting for you.

    I recommend you to scrap either one of these and stick with just one of them. For my example i happend to stick with the setInterval since you're executing the very same method over and over any way.

    The initialization

    Since the presentTime is declared inside the startTimer-function it will be constantly overwritten with 17 + ":" + 00 (resulting in "17:0" btw). This is solved by declaring it in the outer scope instead.

    Remembering the changes

    Finally you need to save the current state of presentTime after modifications. Just adding a presentTime = [m,s].join(":"); at the end of startTimer() solves this.

    var presentTime = "17:00";
    
    function startTimer() {
        var timeArray = presentTime.split(/[:]+/);
        var m = timeArray[0];
        var s = checkSecond((timeArray[1] - 1));
        if (s == 59) {
            m = m - 1
        }
    
        var button2 = document.createElement("Button2");
        var interval = s;
        button2.innerHTML = m + ":" + s;
    
        button2.style = "top:0; left:0rem; height:10% ;color: black; background-color: #ffffff;position:fixed;padding:20px;font-size:large;font-weight: bold;";
    
        document.body.appendChild(button2);
        presentTime = [m,s].join(":");
    }
    
    function checkSecond(sec) {
        if (sec < 10 && sec >= 0) {
            sec = "0" + sec
        }; // add zero in front of numbers < 10
        if (sec < 0) {
            sec = "59"
        };
        return sec;
    }
    
    var interval = setInterval(startTimer, 1000);