Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Setting TimeOut in JavaScript

Status
Not open for further replies.

shyjinka

Programmer
Jul 31, 2003
4
IN
In our web application we are using session which will expire after 30 mns of user inactive time. I want to display an alert message to the user after 25 mn that your session is about to expire within 5 mn. Now it's working fine. But when I'm opening popup windows eventhough the session not expired it's showing "Session about to expire with in 5 mn" to the parent windows. How will i handle this situation. I want to give this alert only if the session is about to out after 5mn.Please help me out. Acutally I'm including this alert message in my header.jsp page
 
Your calculating the time in client side code which is completely disconnected from your server side session timeout. Without synchronizing them wherever that is necessary it is completely impossible. This means that anytime any browser instance of the same session is submitted to the server, all client side instances must be refreshed or the client side timer code manipulated from the other browser instance.

If it seems like a quagmire… it is. I recommend abandoning the concept altogether. Replace it with a completely different paradigm like just providing a simple notification about sessions expiring without trying to track the time in client side code, or using a client side refresh so the session does not expire until the user closes the browser like web email readers do.


-pete
 
If you want any open browser window that views a page on your website not to loose session ID than you can try this:

Assuming that the session times out in 30 minutes and the client can run javascript.
Create an asp page called keepSession.asp with the following content:
<% Response.CacheControl = &quot;no-cache&quot; %>
<% Response.AddHeader &quot;Pragma&quot;, &quot;no-cache&quot; %>
<% Response.Expires = -1 %>
<form id=frm action=&quot;keepSession.asp&quot;>
<input type=text id=txt>
</form>
<script>
// refresh in 1500000 ms (25 minutes)
// set the value of the textbox to the date.getTime to
// generate a unique url so no caching is used by the
// browser
setTimeout(&quot;var dte= new Date();document.getElementById('txt').value= dte.getTime();document.getElementById('frm').submit();&quot;,1500000);
//setTimeout(&quot;var dte= new Date();document.getElementById('txt').value= dte.getTime();document.getElementById('frm').submit();alert(document.cookie);&quot;,1000);
</script>



In all other pages (except keepSession.asp) put the following code:
<IFRAME src=&quot;keepSession.asp&quot; style=&quot;position: absolute;left-1000px;top:-1000px&quot;></IFRAME>



Greetings, Harm Meijer
 
Hi harmmeijer ,
Actually I don't want to refresh the session after 25mn. Actually think is that when a user clicking on a popup window i guess the session will not time out in parent window until the popup window also become inactive. So i need some solutions like the alert message in parent window should not come after 25mn if the session still exist and not reached 25mn in child window. Any way thanks for your reply,
 
Hi, Shyjinka:

How did you solve the problem? I have the same problem now.

Thanks.

Ly
 
Hello all,

Even I am facing the same kind of problem. Do you have any solution for popup a javascript window to alert users a
few minutes before their sessions expire.

Regards
HC
 
A session times out ... seconds after the user makes the last request to the server. The amount of seconds can be set on the web server.

When a window is open it does not automatically mean that the window makes a request to the server. You have to make sure that the window makes a request to the server within the session timeout.

Assuming that the session times out in 30 minutes and the client can run javascript.
Create an asp page called keepSession.asp with the following content:
Code:
<iframe id=RefreshFrame style="width:1px;height:1px"></iframe>
<script>
  // refresh in 1500000 ms (25 minutes)
  var dte= new Date();
  setTimeout("document.getElementById('RefreshFrame').src='keepsession.asp?makesureTheRequestisMade" + dte.getTime() + "'",1500000);
</script>



Greetings, Harm Meijer
 
If your object is to inconvinience the user and not extend the session due to saving recourses on the server or whatever reason you have you can do the following in your pages:
Code:
<script>
function timeoutChecker(){
	// open an html window warning the user that their session is expiering
	// html window will not extend the session because it doesn't go through the server script engine
	this.timer = setTimeout("var win=window.open('alertYourTimeOut.html');win.foucus();",1500000);
	this.extendTimer = function(){
		try{
			clearTimeout(this.timer);
		}catch(e){
		}
		try{
			this.timer = setTimeout("var win=window.open('alertYourTimeOut.html');win.foucus();",1500000);
		}catch(e){
		}
	}
}
var tmr = new timeoutChecker();
</script>

and the following in popups:
Code:
<script>
window.opener.tmr.extendTimer;
</script>




Greetings, Harm Meijer
 
Hi Harmmeijer,

Thanks for your reply. The above solution will display the alert message after every 25 minutes. Even though the session is valid after 25 minutes (user opens a sub window which sends the request to the server), the alert msg will be displayed to the user.

My exact requirement is I need to display the popup msg before 5 minutes of the session expiry. I am having 3 frames (top, right, main) out of which the top frame is static throughout the application. I thought of implementing the session expiry logic in the top frame (top.jsp) since it is static and used throughout the application. Otherwise, I need to implement it in all the jsp files used in the application. I don't want to do that. I can get the last accessed session time using
<%=session.getLastAccessedTime()%>. This value is used in the top frame to calculate the idle time of the browser. The problem is since the top.jsp is static, I can get the session.getLastAccessedTime() when the frame is loaded initally. How can I get the session.getLastAccessedTime() from the jsp (say after every 25 minutes) without reloading the page.

I also thinking of implementing the session popup expiry using the following logic.

From the top frame, if I capture the submit event of the other 2 frames (right, main), then I can get the client's time while submitting the form. Using this I can calculate the idle time. But the problem is the onsubmit event triggers only for the SUBMIT button. In the application, we don't have SUBMIT button. (we are having only href links and buttons to submit the form using document.formname.submit()).

I am capturing the onsubmit event after the </FORM> tag as below.

<SCRIPT language="JavaScript1.2">

function mysubmit() {
alert('Load event fired!');
}

<!--

size = parent.frames.length-1;
for(j=1; j < size; j++){
menuFormName = parent.frames[j].document.forms[0].name;
alert('MENU FORM NAME : ' + menuFormName);
parent.frames[j].document.forms[0].onsubmit = mysubmit;
}

//-->
</SCRIPT>

Any ideas why onsubmit event is not triggered, if I do a document.formname.submit() from the javascript.

Regards
HC
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top