How to use Time Event in JavaScript

In this article I am going to explain about Time Event in JavaScript.
  • 2171

JavaScript Time Event

If we want to execute any function or code on specifics time-interval for its JavaScript provide Time Event by which we can execute function and code on specifics time-interval.

There are two function that used in Time Event.

Time Event function

  • setInterval() -  Execute function on specifics given time. It will be repeat again and again on specifics interval.
  • setTimeout() -  Execute function after waiting on given time.

In this we are execute time function on  specifics  time-interval.

<html>

<head>

    <script type="text/javascript">

        var myVar;

 

        function timeStart() {

            myVar = setInterval(function () { myTimer() }, 1000);

        }

 

        function myTimer() {

            var d = new Date();

            var t = d.toLocaleTimeString();

            document.getElementById("test").innerHTML = t;

        }

 

        function timeStop() {

            clearInterval(myVar);

        }

    </script>

</head>

<body>

    <p>

        press start for start timing</p>

    <button onclick="timeStart()">

        Start time</button>

    <button onclick=" timeStop()">

        Stop time</button>

    <p id="test">

    </p>

</body>

</html>

 

Output

 

 time.gif

Further Readings

You may also want to read these related articles :here

Ask Your Question 

Got a programming related question? You may want to post your question here

Programming Answers here

© 2018 DotNetHeaven. All rights reserved.