Hector Barbossa
Hector Barbossa

Reputation: 5528

Javascript settimeout

I want my users to see one confirm box after say 15 min,alerting them about the session timeout.I want this process to continue repeatedly. That is even if the user selects cancel from the confirm box he will get the same alert after 15 min.

Upvotes: 1

Views: 7658

Answers (5)

tabdulradi
tabdulradi

Reputation: 7596

You have two options

  1. setInterval(f,ms)

    function f() {
    confirm('Session Timeout')
    }
    setInterval(f, 15 * 60 * 1000);

  2. setTimeout(f,ms) + recursion

    function f() {
    confirm('Session Timeout')
    if ( ! stopCondition ) setTimeout(f, 15 * 60 * 1000);
    }
    setTimeout(f, 15 * 60 * 1000);

Conclusion :
setInterval is better when you want the behavior to repeat forever
setTimeout is better when you want to stop the behavior later

Upvotes: 3

ArK
ArK

Reputation: 21068

setInterval(alert(Session Timeout),90000); 

for confirm box

setInterval(alert(confirm('Session Timeout')),90000);

Upvotes: 3

Lee
Lee

Reputation: 13542

just use setTimeout():

function handleSessionTimeout() {
    var isOk = confirm("Your session is going to time out");
    if( isOk ) {
      // let the session time out
    } else {
      // don't let it timeout.
      // restart the timer
      setTimeout(handleSessionTimeout, 900000);
    }
}
setTimeout(handleSessionTimeout, 900000);

the times (900000) are in milliseconds.

Upvotes: 2

Chinmayee G
Chinmayee G

Reputation: 8117

Use setInterval for this

var t  = setInterval("alert('Your sesssion will be expired after 15 min')",900000);

Upvotes: 2

Darin Dimitrov
Darin Dimitrov

Reputation: 1038810

You could use the setInterval function if you want it to run repeatedly:

setInterval(function() {
    alert('hello world');
}, 15 * 60 * 1000);

Also you might take a look at the jquery idleTimer plugin which allows you to detect periods of user inactivity and take some actions.

Upvotes: 5

Related Questions