Computer >> Computer tutorials >  >> Programming >> Javascript

How to use setInterval function call in JavaScript?


The setInterval() method is JavaScript is used to evaluate an expression at intervals. Here’s the syntax:

setInterval(function, interval_in_milliseconds, param1, param2, param3...)

Here, interval_in_milliseconds sets the intervals in milliseconds, after the code will execute.

param are the optional parameters, which is passend to the function.

Example

You can try to run the following code to use setInterval() function call

Live Demo

<!DOCTYPE html>
<html>
   <body>
      <p id="test"></p>
      <script>
         var myVar = setInterval(function(){ displayTimer() }, 500);
         function displayTimer() {
            var date = new Date();
            var time = date.toLocaleTimeString();
            document.getElementById("test").innerHTML = "Time: "+time;
         }
      </script>
    </body>
</html>