Automatically Refresh HTML page or div after some Interval

Automatically Refresh HTML page or div after some Interval

In this post, I will let you know how to refresh HTML page after some time interval automatically.

There are so many ways to refresh page after defined time interval.

You can use the JavaScript function location.reload() to refresh active page.

Example 1: Reload page using JavaScrip setTimeout Method
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4.     <title>Reload page after 5 seconds</title>
  5. <script>
  6. setTimeout(function(){
  7. location.reload();
  8. },5000); // 5000 milliseconds means 5 seconds.
  9. </script>
  10. </head>
  11. <body>
  12. <h1>Welcome to Expert PHP !</h1>
  13. </body>
  14. </html>

Above script will refresh your HTML page after every 5 seconds.

setTimeout() method is used to set a timer which executes a method or specified piece of code after a specified number of milliseconds.

Hint:
1000 ms = 1 second.
Example 2 : Reload page using JavaScrip setInterval Method
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4.     <title>Reload page after 5 seconds</title>
  5. <script>
  6.     function autoRefresh()
  7.     {
  8.         window.location = window.location.href;
  9.     }
  10.      setInterval('autoRefresh()', 5000);
  11.     </script>
  12. </head>
  13. <body>
  14. <h1>Welcome to Expert PHP !</h1>
  15. </body>
  16. </html>

setInterval() method is used to call the function continuously until clearInterval() is called.

Example 3 : Reload page using meta tag in head tag
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4.     <title>Reload page after 5 seconds</title>
  5. <meta http-equiv="refresh" content="5" />
  6. </head>
  7. <body>
  8. <h1>Welcome to Expert PHP !</h1>
  9. </body>
  10. </html>

http-equiv="refresh" will refresh automatically and a content parameter giving the time interval in seconds.

Phone: (+91) 8800417876
Noida, 201301