Auto refresh current page with regular intervals using JS

 

To automatically refresh the current page at regular intervals using JavaScript, you can use the `setTimeout` function to repeatedly reload the page. Here's how you can achieve this:


<!DOCTYPE html>

<html lang="en">

<head>

<meta charset="UTF-8">

<meta name="viewport" content="width=device-width, initial-scale=1.0">

<title>Auto Refresh Page</title>

</head>

<body>

<!-- Your page content goes here -->


<script>

// Function to refresh the page

function autoRefresh() {

  location.reload(); // Reload the current page

}


// Set the interval to refresh the page every 5 seconds (5000 milliseconds)

const refreshInterval = 5000; // Adjust this interval as needed

setTimeout(autoRefresh, refreshInterval);

</script>

</body>

</html>


In the above code, the `autoRefresh` function uses the `location.reload()` method to reload the current page. The `setTimeout` function is used to call the `autoRefresh` function after a specified interval (in milliseconds). In this example, the page will be refreshed every 5 seconds (5000 milliseconds). You can adjust the `refreshInterval` variable to set a different interval.