Full Stack LAMP - MEAN Developer, Python developer. Certified Azure Developer. Freelance programmer/consultant/trainer.

Auto refresh current page with regular intervals using JS

To refresh or reload the active or current pages using JavaScript, you can use the location.reload() ,and setTimeout() methods.

The reload() method does the same as the reload button in your browser.

setTimeout() method calls to evaluate an expression after a specified number of milliseconds.

By default, the reload() method reloads the page from the cache, but you can force it to reload the page from the server by setting the forceGet parameter to true: location.reload(true).

Example script to Auto-refresh current page

Source code of a sample HTML page that reloads the page with 3 seconds regular intervals.

<!doctype html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Bootstrap demo</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.2.0-beta1/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-0evHe/X+R7YkIZDRvuzKMRqM+OrBnVFBL6DOitfPri4tjfHxaWutUpFmBp4vmVor" crossorigin="anonymous">
  </head>
  <body>
    <h1>Hello, world!</h1>
    <script>
	   setTimeout(function(){
	       location.reload();
	   },3000); // 3000 milliseconds means 3 seconds.
   </script>
  </body>
</html>
  1. Ajax has this function that auto updates contents once maybe the database is updated. My question is, does javascript es6 contain the same functionality?

Leave a Reply