How to call a JavaScript function every 5 seconds continuously?

Sometimes, we want to call a JavaScript function every 5 seconds continuously.

In this article, we’ll look at how to call a JavaScript function every 5 seconds continuously.

How to call a JavaScript function every 5 seconds continuously?

To call a JavaScript function every 5 seconds continuously, we call setInterval with the function that we want to run and the interval between runs.

For instance, we write

const interval = setInterval(() => {
  // ...
}, 5000);

clearInterval(interval);

to call setInterval with the callback we want to run and 5000 millisecond period.

Then the callback runs every 5 seconds.

Conclusion

To call a JavaScript function every 5 seconds continuously, we call setInterval with the function that we want to run and the interval between runs.