Categories
JavaScript Answers

How to call a JavaScript function every 5 seconds continuously?

Spread the love

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.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *