Solved

refresh html page at certain interval for the first 5 minutes

Posted on 2011-02-24
6
469 Views
Last Modified: 2012-05-11
How to refresh a html page at certain interval (e.g., every 10 seconds) for the first 5 minutes, after 5 minutes, stop refreshing.  I need code.  Thank you.
0
Comment
Question by:ToneElec
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 9

Expert Comment

by:CCSOFlag
ID: 34973631
you won't be able to do this unless you also integrate server-side scripting.  javascript does not have "session variables" that you can use to store the intervals.  You can easily set it up to refresh every 10 seconds.  The problem is on a refresh it would reset the timer, so there is no way to know when you hit the 5 minute mark unless you use server-side code.  You COULD also do it using a variable in the URL, but it's pretty ugly and would prolly be annoying to the user.
0
 
LVL 82

Accepted Solution

by:
leakim971 earned 250 total points
ID: 34976894
Javascrip have cookies, check this this :
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<script language="javascript">
	window.onload = function() {
		var homer = getCookie("homer");
		var time = (new Date()).getTime();
		var oneMinute = 1000 * 60;
		var dt = 0;
		if(homer==null || homer=="") {
			document.cookie = "homer="+time;
		}
		else {
			dt = time - homer;
		}
		var m = dt / oneMinute;
		if(m<5) { // 5 minutes
			setTimeout("location.reload();", 10 * 1000); // 10 seconds
//			setTimeout("location.replace(location.href);", 10 * 1000);
//			setTimeout("location.href = location.href;", 10 * 1000);
		}
		else {
			document.cookie = "homer=; expires=Thu, 01-Jan-70 00:00:01 GMT;";
		}
		document.getElementById("homer").innerHTML = m;
	}

	function getCookie(c_name) {
		var i,x,y,ARRcookies=document.cookie.split(";");
		for(i=0;i<ARRcookies.length;i++) {
			x = ARRcookies[i].substr(0,ARRcookies[i].indexOf("="));
			y = ARRcookies[i].substr(ARRcookies[i].indexOf("=")+1);
			x = x.replace(/^\s+|\s+$/g,"");
			if(x == c_name) {
				return unescape(y);
			}
		}
	}
</script>
</head>
<body>
<div id="homer"></div>
</body>
</html>

Open in new window

0
 
LVL 9

Expert Comment

by:mayank_joshi
ID: 34977801
Simply write this in <head> </head> section:-

<META HTTP-EQUIV="REFRESH" CONTENT="5">

this will refresh the page every 5 seconds.

0
Don't Cry: How Liquid Web is Ensuring Security

WannaCry is just the start. Read how Liquid Web is protecting itself and its customers against new threats.

 
LVL 9

Expert Comment

by:CCSOFlag
ID: 34979889
you can also do it with cookies as mentioned, but cookies really should be avoided when possible, because a lot of people do not accept cookies.  
0
 

Author Comment

by:ToneElec
ID: 34981856
I tried leakim971's code, not bad code.  

But I have a question, is it possible that just refresh the page every 10 seconds when page was first opened up (pop up), not when the page is refreshed by user.  Right now, I found your code will refresh page every 10 seconds not only when it first opened, but also after user refresh the page manually.  Is this possible?  Thank you.

0
 

Author Comment

by:ToneElec
ID: 34982044
CCSOFlag;
I may have to let user accept cookies, as this will be on Intranet, implementation is more important now.
0

Featured Post

Transaction Monitoring Vs. Real User Monitoring

Synthetic Transaction Monitoring Vs. Real User Monitoring: When To Use Each Approach? In this article, we will discuss two major monitoring approaches: Synthetic Transaction and Real User Monitoring.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
In this tutorial viewers will learn how to style elements, such a divs, with a "drop shadow" effect using the CSS box-shadow property Start with a normal styled element, such as a div.: In the element's style, type the box shadow property: "box-shad…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

695 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question