显示具有服务器端自动时间注销的同步倒计时计时器


Displaying a synchronized countdown timer with server side automatic time logout?

下面是一个PHP会话计时器的例子,它使用AJAX调用来检查"时间"。

我希望能够为用户显示某种简单的javascript倒计时计时器,例如:

function startTimer(duration, display) {
    var timer = duration, minutes, seconds;
    setInterval(function () {
        minutes = parseInt(timer / 60, 10);
        seconds = parseInt(timer % 60, 10);
        minutes = minutes < 10 ? "0" + minutes : minutes;
        seconds = seconds < 10 ? "0" + seconds : seconds;
        display.textContent = minutes + ":" + seconds;
        if (--timer < 0) {
            timer = duration;
        }
    }, 1000);
}
window.onload = function () {
    var length = 60 * 5,
        display = document.querySelector('#time');
    startTimer(length, display);
};

但是CCD_ 1等于php文件中在CCD_。

如何将这些设置为完全同步,并能够将倒计时输出到屏幕?或者我可以仅仅利用php计时而不必创建js计时器吗?

您可以使用php函数time()获得以秒为单位的当前时间,然后将其保存为数据库中的登录时间。每次用户刷新页面时,您都必须向他发送$_SESSION['timeOut'] + ($time_of_login) - time(),它会返回超时的秒数

来自DB 的$time_of_login

所以php文件看起来像:

var length = <? echo $_SESSION['timeOut'] + ($time_of_login) - time(); ?>