Solved

refresh html page at certain interval for the first 5 minutes

Posted on 2011-02-24
6
465 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
Comment Utility
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
Comment Utility
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
Comment Utility
Simply write this in <head> </head> section:-

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

this will refresh the page every 5 seconds.

0
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
LVL 9

Expert Comment

by:CCSOFlag
Comment Utility
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
Comment Utility
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
Comment Utility
CCSOFlag;
I may have to let user accept cookies, as this will be on Intranet, implementation is more important now.
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
(URGENT) Help HTML and CSS Resizing Images 2 19
angularls and plnkr 14 15
SVG Logo 4 16
Apostophes in PHP generated form 6 11
This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
This article discusses four methods for overlaying images in a container on a web page
In this tutorial viewers will learn how to define a gradient in CSS. Create a new HTML document with an internal stylesheet.: Create a div in CSS and name it Gradient. Define the background as "linear-gradient(to right, #ee3668, black)". Ensure you …
HTML5 has deprecated a few of the older ways of showing media as well as offering up a new way to create games and animations. Audio, video, and canvas are just a few of the adjustments made between XHTML and HTML5. As we learned in our last micr…

728 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now