?
Solved

Help with multiple setTimeout's

Posted on 2011-09-05
15
Medium Priority
?
375 Views
Last Modified: 2012-08-14
In my application I'd like to have the following functionality:

1) After 50 minutes without a form submission/reload - show an alert
2) After 60 minutes (10 minutes after step 1), redirect the user to the logic page

To do this I've used two setTimeout's.  However, I am seeing really odd behaviour across different machines:

1) Some users see the alert after about 20 minutes
2) After pressing OK to the alert, sometimes the redirect never happens
3) The page obviously can't redirect if the alert box is still there!

I am going to change this logic to show a modal alert using CSS, however, I'd like to just check the existing code first.  Can anyone advise whether the following code is written correctly to achieve the requirements above?  Or, is there a better approach altogether?

<script type="text/javascript">
/* <![CDATA[ */
setTimeout('Logout()', 3600000); // one hour
function Logout() {
&#9;&#9;window.location = '/login.aspx';
}
setTimeout('DoTimeAlert()', 3000000);
function DoTimeAlert() {
&#9;&#9;var strAlert;
&#9;&#9;strAlert = 'Automatic Logout Alert!\n';
&#9;&#9;strAlert += 'The page you are currently viewing has not been reloaded for 50 minutes. As a security precaution, the application will automatically log you out after 60 minutes of inactivity (e.g. not requesting a new page, or reloading the current page).\n';
&#9;&#9;strAlert += 'You have 10 minutes remaining before being logged out.\n';
&#9;&#9;window.alert(strAlert);
}
/* ]]> */
</script>

Open in new window

0
Comment
Question by:Rouchie
[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
  • 5
  • 4
15 Comments
 
LVL 40

Accepted Solution

by:
Gurvinder Pal Singh earned 1000 total points
ID: 36483550
<script type="text/javascript">
setTimeout('DoTimeAlert()', 3000000); //call DoTimeAlert after 50 min
function DoTimeAlert()
{
                setTimeout('Logout()', 600000); // after 10 min
&#9;&#9;var strAlert;
&#9;&#9;strAlert = 'Automatic Logout Alert!\n';
&#9;&#9;strAlert += 'The page you are currently viewing has not been reloaded for 50 minutes. As a security precaution, the application will automatically log you out after 60 minutes of inactivity (e.g. not requesting a new page, or reloading the current page).\n';
&#9;&#9;strAlert += 'You have 10 minutes remaining before being logged out.\n';
&#9;&#9;window.alert(strAlert);
}

function Logout()
{
&#9;&#9;window.location = '/login.aspx';
}

</script>
0
 
LVL 11

Expert Comment

by:Kusala Wijayasena
ID: 36484204
Instead of javascript alert, use some div based alerts like jquery alert (http://labs.abeautifulsite.net/projects/js/jquery/alerts/demo/) or ExtJs alert (http://dev.sencha.com/deploy/ext-4.0.2a/examples/message-box/msg-box.html). If you used normal javascript alert, it will block the further execution on javascript.

So do that change and check

-Kusala
0
 
LVL 25

Author Comment

by:Rouchie
ID: 36484316
That suggestion has been investigated, and at present its not feasible due to large numbers of users using IE6
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 11

Expert Comment

by:Kusala Wijayasena
ID: 36484336
Are you saying div based approach is not working with IE6 ? i don't think so

-Kusala
0
 
LVL 25

Author Comment

by:Rouchie
ID: 36484344
It works at a basic level, but any decent formatting (e.g. faded opacity backgrounds) do not.  Also, position:static is not supported by IE6 so the dialog box does not stay in correctly.
0
 
LVL 11

Expert Comment

by:Kusala Wijayasena
ID: 36485559
Rouchie,

As i mentioned earlier, normal javascript alert will block your code execution and as a result of it, redirection will not  work

just check the sample that i have attach herewith

-Kusala
test.htm
0
 
LVL 40

Expert Comment

by:Gurvinder Pal Singh
ID: 36486354
Rouchie,

Did you tried my suggestion??
0
 
LVL 25

Author Comment

by:Rouchie
ID: 36487086
@gurvinder372

Yes I tried it thanks.  It seems to work, however, I think kasala's opinion of using a styled dialog is better so that the alert won't prevent the redirect occurring.

@kasala,
In your test.htm file, when you click the OK button, you still get redirected.  It is possible to 'reset' the timeout period so after clicking OK, in another 50 minutes it appears again, and so forth?
0
 
LVL 40

Expert Comment

by:Gurvinder Pal Singh
ID: 36487130
It won't be an issue since as soon as you press OK on that alert box, you will be redirected. So, user won't be able to do anything on that page anyways
0
 
LVL 25

Author Comment

by:Rouchie
ID: 36487141
Okay great thanks gurvinder372.

I'll just wait for kasala's response and then I have two ways of achieving my solution!
0
 
LVL 11

Assisted Solution

by:Kusala Wijayasena
Kusala Wijayasena earned 1000 total points
ID: 36487460
Yes, that is possible. Just do little javascript modification like this:

var timer;
setTimeout('doTimeAlert()', 3000000); 

function doTimeAlert() {
	document.getElementById('mb').style.display = 'block';
	timer = setTimeout('logout()', 600000);
}

function logout() {
	window.location = '/login.aspx';
}

function closeDlg() {
	document.getElementById('mb').style.display = 'none';
	clearTimeout(timer);
	setTimeout('doTimeAlert()', 3000000);
}

Open in new window


-Kusala

0
 
LVL 25

Author Comment

by:Rouchie
ID: 36487594
If I wanted to allow the user to keep pressing OK every 50 minutes, would I need to reference the timer variable again in closeDlg() ?

e.g.

function closeDlg() {
        document.getElementById('mb').style.display = 'none';
        clearTimeout(timer);
        // setTimeout('doTimeAlert()', 3000000);
        timer = setTimeout('doTimeAlert()', 3000000);
}

Open in new window


Is that correct?
0
 
LVL 40

Expert Comment

by:Gurvinder Pal Singh
ID: 36487784
If you want the old timer to close and start a fresh one, then you need the reference to old timer to clear it
0
 
LVL 25

Author Comment

by:Rouchie
ID: 36487798
So my last piece of guesswork above is correct then??
0
 
LVL 40

Expert Comment

by:Gurvinder Pal Singh
ID: 36487908
Yes, that looks correct
0

Featured Post

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

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

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
This article discusses how to implement server side field validation and display customized error messages to the client.
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)
The viewer will learn the basics of jQuery including how to code hide show and toggles. 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…
Suggested Courses

801 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