setInterval is a built-in function in JavaScript that repeatedly calls a function or executes a code snippet at a specified interval. It takes two parameters: the function or code to be executed, and the time interval (in milliseconds) between each execution.
Here is an example of using setInterval in JavaScript:
// HTML code <p id="counter">0</p> // JavaScript code <script> // Function to be executed function incrementCounter() { var counterElement = document.getElementById("counter"); var counterValue = parseInt(counterElement.innerHTML); counterValue++; counterElement.innerHTML = counterValue; } // Calling setInterval to execute the function every 1 second (1000 milliseconds) setInterval(incrementCounter, 1000); </script>
In this example, the incrementCounter function is called every 1 second, and it updates the value of a paragraph element with the id “counter”. The initial value is 0, and it gets incremented by 1 every second.
Note: The code provided above is in HTML format, but it should be saved with a .html extension and opened in a web browser to see the desired output.