
- CSS Tutorial
- CSS - Home
- CSS - Introduction
- CSS - Syntax
- CSS - Inclusion
- CSS - Measurement Units
- CSS - Colors
- CSS - Backgrounds
- CSS - Fonts
- CSS - Text
- CSS - Images
- CSS - Links
- CSS - Tables
- CSS - Borders
- CSS - Margins
- CSS - Lists
- CSS - Padding
- CSS - Cursors
- CSS - Outlines
- CSS - Dimension
- CSS - Scrollbars
- CSS Advanced
- CSS - Visibility
- CSS - Positioning
- CSS - Layers
- CSS - Pseudo Classes
- CSS - Pseudo Elements
- CSS - @ Rules
- CSS - Text Effects
- CSS - Media Types
- CSS - Paged Media
- CSS - Aural Media
- CSS - Printing
- CSS - Layouts
- CSS - Validations
- CSS3 Tutorial
- CSS3 - Tutorial
- CSS3 - Rounded Corner
- CSS3 - Border Images
- CSS3 - Multi Background
- CSS3 - Color
- CSS3 - Gradients
- CSS3 - Shadow
- CSS3 - Text
- CSS3 - Web font
- CSS3 - 2d transform
- CSS3 - 3d transform
- CSS3 - Animation
- CSS3 - Multi columns
- CSS3 - User Interface
- CSS3 - Box Sizing
- CSS Responsive
- CSS - Responsive Web Design
- CSS References
- CSS - Questions and Answers
- CSS - Quick Guide
- CSS - References
- CSS - Color References
- CSS - Web browser References
- CSS - Web safe fonts
- CSS - Units
- CSS - Animation
- CSS Resources
- CSS - Useful Resources
- CSS - Discussion
How to Automatic Refresh a web page in a fixed time?
We can auto-refresh a web page by either using a “meta” tag with the “http-equiv” property, or by using the setInterval() browser API. Automatic refreshing websites have certain use cases to them, for example, while creating a weather-finding web application, we may want to refresh our website after a set interval of time to show the user the near-exact weather data for a location.
Let’s look at the 2 approaches below to understand how to set up an auto-refreshing website.
Approach 1
In this approach, we will use the “http-equiv” property of a “meta” tag to refresh our web application after a particular interval of time which is passed in its “content” attribute. The meta tag is provided to us by default in the HTML5 specification.
Syntax
<meta http-equiv="refresh" content="n">
Here, “n” in a positive integer that refers to the number of seconds to refresh the page after.
Example
In this example, we will use the “http-equiv” property of a “meta” tag to refresh our web application after every 2 seconds.
<!DOCTYPE html> <html lang="en"> <head> <title>How to Automatic Refresh a web page in fixed time?</title> <meta http-equiv="refresh" content="2"> </head> <body> <h3>How to Automatic Refresh a web page in fixed time?</h3> </body> </html>
Approach 2
In this approach, we will use the “setInterval()” API is given to us by the browser, which allows us to run a certain piece of code after every certain amount of time passed, both of which are passed as arguments to the browser API.
Syntax
setInterval(callback_fn, time_in_ms)
“setInterval()” takes 2 parameters, first is a callback function that triggers after the delay, and the second is the delay provided in milliseconds.
Example
In this example, we will use the “setInterval()” browser API to refresh our web application after every 2 seconds.
<!DOCTYPE html> <html lang="en"> <head> <title>How to Automatic Refresh a web page in fixed time?</title> </head> <body> <h3>How to Automatic Refresh a web page in fixed time?</h3> <script> window.onload = () => { console.clear() console.log('page loaded!'); setInterval(() => { window.location = window.location.href; }, 2000) } </script> </body> </html>
Conclusion
In this article, we learned how to automatically refresh our web application after a fixed amount of time using both, HTML5 and javascript using two different approaches. In the first approach, we used the “http-equiv” property of the “meta” tag, and in the second approach, we used the “setInterval” browser API.
- Related Articles
- How to refresh a page in Firefox?
- How to refresh a JSP page at regular interval?
- How to Use Meta Refresh Redirects for Page Redirects?
- How to automatically redirect a Web Page to another URL?
- How to automatically transfer visitors to a new web page?
- How should I initialize jQuery in a web page?
- How to refresh a browser then navigate to a new page with Javascript executor in Selenium with python?
- How to play sound file in a web-page in the background?
- How to design a calendar for a web page using jQuery EasyUI?
- How to get the web page contents from a WebView in Android?
- Smart Ways to Save a Web Page Forever!!
- How to scroll a Web Page using coordinates of a WebElement in Selenium WebDriver?
- How to download all images on a web page at once?
- Save a Web Page with Python Selenium
- How to add an image as background image of a web page?
