Categories
JavaScript Answers

How to Reload a Page Every 5 Seconds with JavaScript?

Spread the love

Sometimes, we want to reload a page every 5 seconds in our web app.

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

Using the setInterval Function

We can use the setInterval function to run code periodically.

It takes the callback with the code to run periodically as the first argument and the 2nd argument is the delay between each time the callback runs.

For instance, if we have:

<div>
  hello world
</div>

Then we can write:

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

to call window.location.reload event 5 seconds.

The delay is in milliseconds so 5000 ms is 5 seconds.

Now we should see the page reload every 5 seconds.

Conclusion

We can use the setInterval function to run code to reload the page periodically.

window.location.reload() will reload the page.

Leave a Reply

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