会话超时计数器

时间:2018-10-04 15:37:19

标签: javascript jquery

我正试图编写一个会话超时计数器,以便在闲置15分钟后删除客户篮,到目前为止,我所拥有的一半代码都可以正常工作,但是我无法解决如何创建计数器来跟踪时间。我将时间设置为较低的值进行测试

$(document).ready(function() {
    var sessionTimeoutWarning =1;
    var sessionTimeout = 3;
    var sTimeout = parseInt(sessionTimeoutWarning) * 60 * 1000;

    var timeOnPageLoad = new Date();
    setTimeout(sessionWarning(), sTimeout);

    function sessionWarning() {
        var minutesForExpiry = (parseInt(sessionTimeout) - parseInt(sessionTimeoutWarning));
        var message = "Your session will expire in another " + minutesForExpiry + " mins! Please continue browsing before your basket is deleted.";
        alert(message);
        var currentTime = new Date();
        var timeForExpiry = timeOnPageLoad.setMinutes(timeOnPageLoad.getMinutes() + parseInt(sessionTimeout));
        if (Date.parse(currentTime) > timeForExpiry) {
            alert("Session expired. Items in the basket will be deleted.");
            window.location = "/";
        }
    }
});

任何帮助将不胜感激

0 个答案:

没有答案