user2223806
user2223806

Reputation: 11

Javascript alert suppresses other javascript functions

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

Answers (3)

karthick
karthick

Reputation: 12176

alert() and confirm() are two prompts which stops the java script execution.

Upvotes: 0

Parthik Gosar
Parthik Gosar

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

Koen Betsens
Koen Betsens

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

Related Questions