Menu

Auto Refresh / Reload Page Using Javascript Or Meta Tag

Hey Friends,
Today we are going to learn about reloading or refreshing web pages. Suppose we need to get fresh data or the latest data after a particular time frame. Then we can auto-refresh the data by reloading or refreshing the page. I will show you how to refresh the page after every 5 seconds.
I will show you multiple methods to reload the page.

Using html meta tag

Our First Method is a Meta tag. we need to use http-equiv attr to auto-reload the page. value of this attr is refresh. Let me know an example of this.

<!DOCTYPE html>
<html>
<head>
	<title>TemplateBench | Page Reload after 5 seconds</title>
	<meta http-equiv="refresh" content="5" />
</head>
<body>
<h1>TemplateBench Home page</h1>
</body>
</html>

Using setInterval method

Our second method is setInterval. this is a javascript method. The setInterval() method calls a function at specified intervals (in milliseconds). Let's check, How we can use the setInterval() method to refresh the page after every 5 seconds.

<!DOCTYPE html>
<html>
<head>
	<title>TemplateBench | Page Reload after 5 seconds</title>
</head>
<body>
	<h1>TemplateBench Home page</h1>
</body>
<script type="text/javascript">
	setInterval(reloadPage, 500);

	function reloadPage() {
		window.location = window.location.href;
	}
</script>
</html>

Using  setTimeout method

Our second method is setInterval. this is a javascript method. The setTimeout() method calls a function after a number of milliseconds. Let's check, How we can use the setTimeout() method to refresh the page after every 5 seconds.

<!DOCTYPE html>
<html>
<head>
		<title>TemplateBench | Page Reload after 5 seconds</title>
</head>
<body>

	<h1>TemplateBench Home page</h1>

</body>


<script type="text/javascript">
   setTimeout(function(){
       location.reload();
   },5000);
</script>
</html>

These three are the most used methods for reloading the page. 
I hope these methods are useful for you.
Don't forget to join our social handlers.
Thanks

711
Search

Ads