VOOZH about

URL: https://www.geeksforgeeks.org/javascript/how-to-automatic-refresh-a-web-page-in-fixed-time/

⇱ How to Automatic Refresh a Web Page in a Fixed Time? - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

How to Automatic Refresh a Web Page in a Fixed Time?

Last Updated : 23 Jul, 2025

Automatically refreshing a web page at a fixed time interval is useful for keeping content up-to-date without requiring manual user intervention. This can be particularly helpful for displaying real-time information.

There are two main methods to achieve this: using the HTML <meta> tag or JavaScript's setInterval() function. Let's explore each method in detail:

1. Using a "meta" Tag

The meta tag in HTML can be used to automatically refresh a web page at specified intervals. By setting the http-equiv attribute to "refresh" and the content attribute to the desired time in seconds, the page will reload at the defined interval.

Syntax

<meta http-equiv="refresh" content="(time in seconds)">

In this example the the <meta http-equiv="refresh" content="10"> tag automatically refreshes the webpage every 10 seconds, reloading the content without user interaction, as specified by the content="10" attribute.

Output:

👁 Realoding-2-second
Auto Refresh with meta Tag

2. Using the setInterval() Method

An alternative method to implement automatic page refresh is by using JavaScript's setInterval() function. Until clearInterval() is called to stop it, setInterval() will continuously invoke the specified function at regular intervals, effectively providing an auto-refresh behavior on the webpage.

Syntax:

<script>
function autoRefresh() {
window.location = window.location.href;
}
setInterval('autoRefresh()', 5000);
</script>

In this example, the setInterval() method is used to call the autoRefresh() function every 2 seconds, causing the page to reload continuously at that interval.

Output:

👁 Realoding-2-second
Reloading Page

Which method to use?

Control and Flexibility:

  • <meta> Tag: Simple and easy to implement but less flexible. Once set, it cannot be easily modified without reloading the page.
  • setInterval(): Provides greater control and flexibility. You can adjust the refresh interval dynamically, stop the refresh with clearInterval(), or even add conditions to determine when the refresh should occur.

Compatibility:

  • <meta> Tag: Works directly in HTML without needing any scripts, making it a straightforward option compatible with all browsers.
  • setInterval(): Requires JavaScript, so if JavaScript is disabled in the browser, this method will not work.

Performance:

  • <meta> Tag: Simple but refreshes the entire page, which may be less efficient for larger or complex pages.
  • setInterval(): Can be more efficient if combined with AJAX to refresh only specific parts of the page instead of the whole page.

Both the <meta> tag and JavaScript's setInterval() method provide easy ways to implement automatic page refreshes, each with its own advantages. The <meta> tag is ideal for basic use cases where simple and fixed intervals are sufficient. Meanwhile, setInterval() offers greater flexibility and control, making it suitable for more dynamic and complex applications.

Comment