Automatically refresh or reload a page using JavaScript
If you want to reload or refresh the page automatically after a certain time, you can do it using JavaScript. In this article, you will see three different methods to automatically refresh or reload a page.
Checkout more articles on JavaScript
- typeof operators in JavaScript
- How to generate a Unique ID in JavaScript
- How to insert an element after another element in JavaScript
- Execute code only after all images have been loaded in JavaScript
Different methods to auto refresh or reload a page
1. Using setTimeout
If you want to reload a page after a certain time then you can use the `setTimeout` function.
<title>Page will reload after 5 seconds - Clue Mediator</title>
<h1>Page will reload after 5 seconds - Clue Mediator</h1>
<script type="text/javascript">
setTimeout(function () {
location.reload();
}, 5000);
</script>
In the second parameter, we have to pass the milliseconds so that the written function will be executed after the given milliseconds.
2. Using setInterval
If you want to reload a page after every given seconds then you can use the `setInterval` function.
<title>Page will reload after every 5 seconds - Clue Mediator</title>
<h1>Page will reload after every 5 seconds - Clue Mediator</h1>
<script type="text/javascript">
setInterval(function () {
location.reload();
}, 5000);
</script>
Same as the `setTimeout`, we have to pass the milliseconds in the second parameter.
3. Using meta tag
In this method, you can simply use the `http-equiv` meta tag to refresh the content.
<meta http-equiv="refresh" content="5">
<title>Page will reload after every 5 seconds - Clue Mediator</title>
<h1>Page will reload after every 5 seconds - Clue Mediator</h1>
That’s it for today.
Thank you for reading. Happy Coding..!! 🙂