In JavaScript, you can use the setInterval()
method to run a function repeatedly at a specified interval. Here’s an example of how you can use setInterval()
to run a function every second:
function displayTime() {
console.log(new Date());
}
setInterval(displayTime, 1000);
In this example, the displayTime()
function is defined and will be run every 1000 milliseconds (1 second) by setInterval()
. The current date and time will be logged to the console every second.
Please note that setInterval()
continues to run the function until it is explicitly stopped using clearInterval()
. If you don’t need the function to run indefinitely, you may want to use a counter and clear the interval after a specified number of iterations.
+ There are no comments
Add yours