Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

JavaScript functions

Biju_PhilipBiju_Philip TXMember Posts: 4

Can I use the clearInterval() function to stop a time display for a countdown clock? Is there a function available to reset the clock or do I have to create my own function?

Answers

  • Nihar MoreNihar More Member Posts: 3

    Display the current time (the setInterval() method will execute the function once every 1 second, just like a digital watch). Use clearInterval() to stop time:

    var myVar = setInterval(function(){ myTimer() }, 1000);
    
    function myTimer() {
        var d = new Date();
        var t = d.toLocaleTimeString();
        document.getElementById("demo").innerHTML = t;
    }
    
    function myStopFunction() {
        clearInterval(myVar);
    }
    

    Definition and Usage
    The clearInterval() method clears a timer set with the setInterval() method.

    The ID value returned by setInterval() is used as the parameter for the clearInterval() method.

    Note: To be able to use the clearInterval() method, you must use a global variable when creating the interval method:

    myVar = setInterval("javascript function", milliseconds);
    Then you will be able to stop the execution by calling the clearInterval() method.
    Learn JavaScript here - https://hackr.io/tutorials/learn-javascript

Sign In or Register to comment.