Solved

refresh html page at certain interval for the first 5 minutes

Posted on 2011-02-24
6
467 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
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
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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.

 
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Use these top 10 tips to master the art of email signature design. Create an email signature design that will easily wow recipients, promote your brand and highlight your professionalism.
This article discusses how to create an extensible mechanism for linked drop downs.
In this tutorial viewers will learn how to position overlapping items using z-index in CSS. They will also learn the restrictions on the z-index property.  Create a new HTML document with an internal stylesheet.: Create a div in CSS and name it Red.…
In this tutorial viewers will learn how to code links for mobile sites that, once clicked, send a call or text to a specified number. For a telephone link (once clicked, calls a number), begin with a normal "<a href=" link tag. For the href, specify…

830 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