douyinmian8151 2016-05-20 06:18
浏览 82
已采纳

如果javascript计时器正在运行,请防止会话超时

I have a web application made in Code igniter. What it basically does is -

  1. user logs in to the application
  2. User can do various tasks, one of which is timer based tasks. There is a button which starts the javascript timer
  3. While timer is running he performs certain tasks, and once he is done, he stops the timer. The timer may run for few minutes to few hours.

My Question - While JavaScript timer is running, can we prevent session timeout, so that users are not logged out and their progress is not lost?? My initial thought is that it can't be, at least there isn't an easy way because we are dealing with Javascript timer (Client) and the Session timeouts (server).

Thanks in advance

  • 写回答

1条回答 默认 最新

  • dongtaidai0492 2016-05-20 06:23
    关注

    Throughout the timer session keep sending keepalive request every some minutes(preferred 2 to 5 minutes)

    This keepalive request will be the as light as possible and will only get session on the server and will keep it alive

    The response will be also as simple as something like session is still active kind of thing...

    On the other hand you could also maintain a variable on javascript side usersLastActivity, which is updated on each document mousemove or document keydown and few events. If there's been any activity since last request, then send keepalive request ...

    To get more idea about you can have a look at other same kind of question posted here.

    The basic example:

    setInterval(function(){
       $.get('/ImStillAlive.action');
    }, 300000); // 5 mins * 60 * 1000
    

    With basic check for typing activity:

    $(function(){
        var lastUpdate = 0;
        var checkInterval = setInterval(function(){
           if(new Date().getTime() - lastUpdate > 300000){
               clearInterval(checkInterval);
           }else{   
                $.get('/ImStillAlive.action');
           }
        }, 300000); // 5 mins * 60 * 1000
    
        $(document).keydown(function(){
             lastUpdate = new Date().getTime();
        });
    });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥60 请查询全国几个煤炭大省近十年的煤炭铁路及公路的货物周转量
  • ¥15 请帮我看看我这道c语言题到底漏了哪种情况吧!
  • ¥66 如何制作支付宝扫码跳转到发红包界面
  • ¥15 pnpm 下载element-plus
  • ¥15 解决编写PyDracula时遇到的问题
  • ¥15 有没有人能解决下这个问题吗,本人不会编程
  • ¥15 plotBAPC画图出错
  • ¥30 关于#opencv#的问题:使用大疆无人机拍摄水稻田间图像,拼接成tif图片,用什么方法可以识别并框选出水稻作物行
  • ¥15 Python卡尔曼滤波融合
  • ¥20 iOS绕地区网络检测