Last updated: Mar 7, 2024
Reading time·4 min
http-equiv
directive to auto-refresh the page every N secondssetTimeout()
function to auto-refresh the page every N secondswindow.onload
event to auto-refresh a page every N secondsTo auto-reload a page every N seconds in JavaScript:
setInterval()
method to run a function every 1000 milliseconds.window.location.reload()
method.Here is the HTML for the example.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> </head> <body> <h2>bobbyhadz.com</h2> <h2 id="refresh-timer"></h2> <script src="index.js"></script> </body> </html>
And here is the related JavaScript code.
const refreshTimer = document.getElementById('refresh-timer'); let timerInSeconds = 0; setInterval(() => { timerInSeconds += 1; refreshTimer.innerHTML = `Refreshing page in 3 sec: ${timerInSeconds} seconds`; if (timerInSeconds >= 3) { window.location.reload(); } }, 1000);
We used the document.getElementById()
method to select the refresh-timer
element by its ID.
We could've also used the document.querySelector() method to achieve the same result.
const refreshTimer = document.querySelector('#refresh-timer');
We used the setInterval() method to repeatedly call a function every N milliseconds.
We called the function every 1000 milliseconds (1 second) to update the
innerHTML
property of the refresh-timer
element.
const refreshTimer = document.getElementById('refresh-timer'); let timerInSeconds = 0; setInterval(() => { timerInSeconds += 1; refreshTimer.innerHTML = `Refreshing page in: ${timerInSeconds} seconds`; if (timerInSeconds >= 3) { window.location.reload(); } }, 1000);
Each time the callback function is invoked:
innerHTML
property of the refresh timer element to display
how many seconds remain until the page is refreshed.The example refreshes the page every 3 seconds, however, you can adjust this depending on your use case.
The following code sample refreshes the page every 5 seconds.
const refreshTimer = document.getElementById('refresh-timer'); let timerInSeconds = 0; setInterval(() => { timerInSeconds += 1; refreshTimer.innerHTML = `Refreshing page in: ${timerInSeconds} seconds`; if (timerInSeconds >= 5) { window.location.reload(); } }, 1000);
Once the timer reaches the specified number of seconds, the page is auto-reloaded using the window.location.reload() method.
The location.reload()
method reloads the current URL in the same way the
Refresh button does.
http-equiv
directive to auto-refresh the page every N secondsYou can also use the http-equiv directive to auto-reload the page every N seconds.
The following example doesn't require you to use JavaScript.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <!-- 👇️ Auto-refresh every 3 seconds --> <meta http-equiv="refresh" content="3" /> </head> <body> <h2>bobbyhadz.com</h2> <h2 id="refresh-timer"></h2> </body> </html>
Adding the following meta
tag to the head
section of your HTML page
auto-refreshes the page every 3 seconds.
<meta http-equiv="refresh" content="3" />
When the http-equiv
attribute is set to refresh
, the browser is instructed
to reload the page after the specified content
seconds.
The content
attribute is set to 3
, so the page gets reloaded after 3
seconds.
setTimeout()
function to auto-refresh the page every N secondsYou can also use the setTimeout()
function to auto-refresh the page every N
seconds.
Here is the HTML for the example.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> </head> <body> <h2>bobbyhadz.com</h2> <script src="index.js"></script> </body> </html>
And here is the related JavaScript code.
setTimeout(() => { window.location.reload(); }, 3000);
The setTimeout() method sets a timer that executes a function after a specified delay.
Note that the delay is specified in milliseconds.
1000 milliseconds = 1 second, so using a value of 3000 milliseconds refreshes the page every 3 seconds.
window.onload
event to auto-refresh a page every N secondsYou can also use the window.onload event to auto-refresh a page every N seconds.
Here is the HTML for the example.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> </head> <body> <h2>bobbyhadz.com</h2> <h2 id="refresh-timer"></h2> <script src="index.js"></script> </body> </html>
And here is the related JavaScript code.
window.onload = function refreshEveryNSeconds() { const refreshTimer = document.getElementById('refresh-timer'); let timerInSeconds = 0; setInterval(() => { timerInSeconds += 1; refreshTimer.innerHTML = `Refreshing page in: ${timerInSeconds} seconds`; if (timerInSeconds >= 3) { window.location.reload(); } }, 1000); };
The load
event is triggered when the whole page has loaded, including the
page's stylesheets, scripts, iframes and images.
Waiting for the load
event might be necessary if you want to render the entire
page before you start the timer.
setInterval()
method to run some code every 1 second (1000
milliseconds).innerHTML
attribute
of the refresh timer element to display the time before the page is
refreshed.window.location.reload()
method to refresh the page.I've also written an article on how to refresh an image with a new one at the same URL using JS.
You can learn more about the related topics by checking out the following tutorials: