简体   繁体   English

用户登录后如何显示计时器?

[英]How to show timer after user logged?

I want to show timer after user login here is code : 我想在用户登录后显示计时器是代码:

Demo : Fiddle 演示: 小提琴

function startTimer_session()
{   
    var today = new Date();            
    var h=today.getHours() - login.getHours();
    var m=today.getMinutes() - login.getMinutes();
    var s=today.getSeconds() - login.getSeconds();
    // add a zero in front of numbers<10
    h=checkTime(h);
    m=checkTime(m);
    s=checkTime(s);
    $('#user-timer').html(h+":"+m+":"+s);
    timer=setTimeout(function(){startTimer_session()},1000);
}

but it does not show properly, why? 但显示不正确,为什么?

The problem is that today.getSeconds is less that login.getSeconds. 问题在于,today.getSeconds小于login.getSeconds。

You need to do the subtraction on the full date and then take the parts. 您需要对整个日期进行减法,然后进行零件分解。

The smallest change is: 最小的变化是:

    var today = new Date(); 
    var d = new Date(today - login);
    var h = d.getHours();
    var m = d.getMinutes();
    var s = d.getSeconds();

Or use the date formatting that Satpal showed 或使用Satpal显示的日期格式

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM