?
Solved

refresh html page at certain interval for the first 5 minutes

Posted on 2011-02-24
6
Medium Priority
?
473 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 1000 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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?
When crafting your “Why Us” page, there are a plethora of pitfalls to avoid. Follow these five tips, and you’ll be well on your way to creating an effective page.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.
Suggested Courses

770 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