How to reload a page every X seconds with JavaScript?

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.