使用javascript减少会话时间值并分配给标签文本

时间:2013-12-03 05:34:08

标签: javascript asp.net

我已经使用此函数从web.config文件中获取session timeout

function readwebconfig() {
        var sessiontime = "<%= Session.Timeout %>";
        document.getElementById('lblmsg').innerHTML = sessiontime;
    }

它返回一些数值,现在我希望使用javascript decrement这个值。并显示此值,如9Min :30Sec's.

提前致谢。

1 个答案:

答案 0 :(得分:1)

由于会话超时以分钟为单位,首先使用会话超时值添加当前日期,然后向用户显示倒计时。

<强> JS: -

var sessionTimeout = <%= Session.Timeout %> ;
var now = new Date();
var kickoff = now.setMinutes(now.getMinutes() + parseInt(sessionTimeout));

function updateWCTime() {
    now = new Date();

    diff = kickoff - now;

    days = Math.floor(diff / (1000 * 60 * 60 * 24));
    hours = Math.floor(diff / (1000 * 60 * 60));
    mins = Math.floor(diff / (1000 * 60));
    secs = Math.floor(diff / 1000);

    mm = mins - hours * 60;
    ss = secs - mins * 60;

    document.getElementById("txt")
        .innerHTML = mm + ' minutes ' + ss + ' seconds';
}
setInterval(updateWCTime, 1000);

<强> HTML: -

<div id="txt">
    </div>