Table of Contents In this tutorial, you will learn how to auto refresh web page every 5 seconds using javascript and HTML. Auto refresh also known as auto reload. Generally, we auto refresh any web page either by using the F5 key on our keyboard or by clicking on reload option in the context menu. Auto refreshing a web page programmatically could be tricky for most newbie developers and today, I am going to share my approach on this. There are 2 possible solutions to auto refresh a web page. The first solution involves only HTML and the second solution involves only javascript. Depending upon your requirement, you can choose one out of these two. For the HTML auto refresh solution, we will make use of
For the javascript auto refresh solution, we will make use of In the following example, we will cover both the solution mentioned above. We will simply display a counter of 5 seconds on the screen and as soon as it completes, the auto refresh will be triggered. Please have a look over the code example and the steps given below.
HTML & CSS
HTML Auto Refresh
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <!-- <meta http-equiv="refresh" content="5"> --> <title>Document</title> </head> <body> <h2 style="text-align: center">Counter</h2> <script src="script.js"></script> </body> </html> Javascript Auto Refresh
let counter = 1; setInterval(() => { document.querySelector('h2').innerText = counter; counter++; if(counter > 5) location.reload(); }, 1000); View Discussion Improve Article Save Article View Discussion Improve Article Save Article Suppose we have given an HTML document and the task is to automatically refresh the webpage after a certain period of time in the web browser. We will predefine a time period and the browser automatically refreshes the webpage. Example: You are creating an auto refreshing website that needs to be refreshed after a certain smaller period of time. So, in this case, you can use the meta http-equiv tag to refresh the webpage. Another illustration of this http-equiv tag is that it can be used to reload a weather website that needs to be updated after every small interval of time to show the minute weather changes. Approach 1: One can auto refresh the webpage using the meta tag within the head element of your HTML using the http-equiv property. It is an inbuilt property with HTML 5. One can further add the time period of the refresh using the content attribute within the Meta tag. The HTTP equiv attribute can be used to simulate an HTTP response header. The attribute is supported by all major web browsers such as Google Chrome, Mozilla Firefox, Microsoft Edge, Safari, Opera Mini, etc. Syntax: <meta http-equiv="refresh" content="10"> Example:
Output: Approach 2: Using setInterval() method: Another method to access the auto refresh property of webpage is by using the following JavaScript code until ClearInterval() is called, setInterval() will continue to call itself continuously. Syntax: <script> function autoRefresh() { window.location = window.location.href; } setInterval('autoRefresh()', 5000); </script> Example:
Output: How do I make HTML refresh automatically?Approach 1: One can auto refresh the webpage using the meta tag within the head element of your HTML using the http-equiv property. It is an inbuilt property with HTML 5. One can further add the time period of the refresh using the content attribute within the Meta tag. How do I refresh every 10 seconds?“javascript reload page after 10 seconds” Code Answer. <script>. window. setInterval('refresh()', 10000);. // Call a function every 10000 milliseconds.. // (OR 10 seconds).. // Refresh or reload page.. function refresh() {. window . location. reload();. How do I refresh a web page every 5 seconds?Open the web page that you want to automatically refresh at certain seconds of the interval. Then, click on the extension icon in your Chrome bar and select the interval time. Can you set a web page to automatically refresh?It's as simple as going to your browser's app/extension store and finding one you like: Launch your browser. Go to app/extension store (Chrome Web Store, Firefox Add-Ons, Microsoft Edge Add-ons Store, etc.). Enter “auto-refresh” in the search bar. |