Reputation: 11
I have the following problem: I want to warn a user about an upcoming session timeout with an alert. When the actual session timeout happens, I want to pop-up another alert and forward to any website. My approach works with Firefox and IE. With Safari and Chrome, when I don't submit the first alert before the second timeout happens, the second function is never executed, actually. Any hints?
setTimeout(function() {
alert('Session timeout in 5 minutes...');
}, 30 * 1000);
setTimeout(function() {
alert('Session timeout, sorry');
location.href = 'http://google.com';
}, 60 * 1000);
Upvotes: 1
Views: 133
Reputation: 12176
alert() and confirm() are two prompts which stops the java script execution.
Upvotes: 0
Reputation: 11646
This issue would be solved by creating a custom alert box. But Incase you want to do it with the broswer alert box, you can try this.
var nStartTime = new Date().getTime();
setTimeout(function() {
alert('Session timeout in 5 minutes...');
var nRemainingTime = 60 * 1000 - (new Date().getTime() - nStartTime);
nRemainingTime = nRemainingTime < 0 ? 0 : nRemainingTime ;
setTimeout(function() {
alert('Session timeout, sorry');
location.href = 'http://google.com';
}, nRemainingTime);
}, 30 * 1000);var nStartTime = new Date().getTime();
Upvotes: 0
Reputation: 88
An alert pauses your javascript execution. It's considered more UI friendly to use a Growl or top-bar message type of notification.
edit:
There are loads of libraries for this, but that's part of another question.
Growl style notifications that are library/framework independent?
Upvotes: 1