如果JavaScript计时器正在运行,则阻止会话超时 [英] Prevent session time out if javascript timer is running

查看:160
本文介绍了如果JavaScript计时器正在运行,则阻止会话超时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个在代码igniter中创建的web应用程序。它基本上是 -


  1. 用户登录到应用程序

  2. 用户可以执行各种任务,其中一个是基于计时器的任务。有一个启动javascript计时器的按钮

  3. 当计时器运行时,他执行某些任务,一旦完成,他停止计时器。


  4. 我的问题 - 当JavaScript计时器正在运行时,我们可以阻止会话超时,因此,用户没有注销,他们的进度不会丢失?我最初的想法是,它不能,至少没有一个简单的方法,因为我们处理Javascript计时器(客户端)和会话超时(服务器)。



    提前感谢

    解决方案

    在整个计时器会话中,每隔几分钟(优选2到5分钟)保持发送keepalive请求



    这个keepalive请求将尽可能的轻,并且只会在服务器上获取会话,并保持它的活动



    响应也会像会话仍然是活动的东西那样简单...



    另一方面,你也可以在javascript侧保持一个变量usersLastActivity,它在每个文档mousemove或文档keydown和少数事件上更新。如果自上次请求以来有任何活动,则发送keepalive请求...



    要更好地了解你可以看看其他同类问题这里



    基本示例:

      setInterval(function b $ b $ .get('/ ImStillAlive.action'); 
    },300000); // 5分钟* 60 * 1000

    输入活动的基本检查:

      $(function(){
    var lastUpdate = 0;
    var checkInterval = setInterval if(new Date()。getTime() - lastUpdate> 300000){
    clearInterval(checkInterval);
    } else {
    $ .get('/ ImStillAlive.action');
    }
    },300000); // 5 mins * 60 * 1000

    $(document).keydown(function(){
    lastUpdate = new Date .getTime();
    });
    });


    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

    解决方案

    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();
        });
    });
    

    这篇关于如果JavaScript计时器正在运行,则阻止会话超时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆