Solved

Help with multiple setTimeout's

Posted on 2011-09-05
15
348 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
  • 6
  • 5
  • 4
15 Comments
 
LVL 40

Accepted Solution

by:
gurvinder372 earned 250 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
 
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:gurvinder372
ID: 36486354
Rouchie,

Did you tried my suggestion??
0
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
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:gurvinder372
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 250 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:gurvinder372
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:gurvinder372
ID: 36487908
Yes, that looks correct
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
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.
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…

708 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

11 Experts available now in Live!

Get 1:1 Help Now