Categories
JavaScript Answers

How to reload a page every X seconds with JavaScript?

Spread the love

Sometimes, we want to reload a page every X seconds with JavaScript.

In this article, we’ll look at how to reload a page every X seconds with JavaScript.

How to reload a page every X seconds with JavaScript?

To reload a page every X seconds with JavaScript, we can use the setInterval function.

For instance, we write:

setInterval(() => {
  location.reload(true);
}, 5000);

to call setInterval with a function that calls location.reload to reload the page.

The 2nd argument is the interval to wait until the callback is called again.

Conclusion

To reload a page every X seconds with JavaScript, we can use the setInterval function.

By John Au-Yeung

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

Leave a Reply

Your email address will not be published.