最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Javascript settimeout - Stack Overflow

programmeradmin3浏览0评论

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.

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.

Share Improve this question asked Nov 2, 2010 at 7:16 Hector BarbossaHector Barbossa 5,52813 gold badges50 silver badges70 bronze badges 1
  • 1 Yep, just click the little check mark next to the best answer. It makes folks want to answer you more. – rwilliams Commented Nov 2, 2010 at 7:57
Add a ment  | 

5 Answers 5

Reset to default 5

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.

setInterval(alert(Session Timeout),90000); 

for confirm box

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

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

Use setInterval for this

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

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.

发布评论

评论列表(0)

  1. 暂无评论