【问题标题】:Setting cookie to expire after one day将 cookie 设置为一天后过期
【发布时间】:2020-10-08 21:19:24
【问题描述】:

我使用了从 git 获得的这段代码。它基本上设置为设置一个 cookie 以仅在第一次访问该站点时显示一个弹出窗口。但是,我希望它只设置 24 小时。因此,如果有人在一两天内返回该站点,它将再次显示。

(function ($) {

    'use strict';

    $.fn.firstVisitPopup = function (settings) {

        var $body = $('body');
        var $dialog = $(this);
        var $blackout;
        var setCookie = function (name, value) {
            var date = new Date(),
                expires = 'expires=';
            date.setTime(date.getTime() + 31536000000);
            expires += date.toGMTString();
            document.cookie = name + '=' + value + '; ' + expires + '; path=/';
        }
        var getCookie = function (name) {
            var allCookies = document.cookie.split(';'),
                cookieCounter = 0,
                currentCookie = '';
            for (cookieCounter = 0; cookieCounter < allCookies.length; cookieCounter++) {
                currentCookie = allCookies[cookieCounter];
                while (currentCookie.charAt(0) === ' ') {
                    currentCookie = currentCookie.substring(1, currentCookie.length);
                }
                if (currentCookie.indexOf(name + '=') === 0) {
                    return currentCookie.substring(name.length + 1, currentCookie.length);
                }
            }
            return false;
        }
        var showMessage = function () {
            $blackout.show();
            $dialog.show();
        }
        var hideMessage = function () {
            $blackout.hide();
            $dialog.hide();
            setCookie('fvpp' + settings.cookieName, 'true');
        }

        $body.append('<div id="fvpp-blackout"></div>');
        $dialog.append('<a id="fvpp-close">&#10006;</a>');
        $blackout = $('#fvpp-blackout');

        if (getCookie('fvpp' + settings.cookieName)) {
            hideMessage();
        } else {
            showMessage();
        }

        $(settings.showAgainSelector).on('click', showMessage);
        $body.on('click', '#fvpp-blackout, #fvpp-close', hideMessage);

    };

})(jQuery);

【问题讨论】:

  • 所以将 setTime 改为一天
  • 数字 31536000000 是以毫秒为单位的年份,因此将其更改为以毫秒为单位的 1 天

标签: javascript jquery cookies


【解决方案1】:

变化:

date.setTime(date.getTime() + 31536000000);

到:

date.setDate(date.getDate() + 1);

这会将日期增加 1 天。旧代码增加了 365 天。

【讨论】:

    【解决方案2】:

    日期 setTime 函数需要以毫秒为单位的时间。 在下面的示例中,cookie 将在大约 6 个月后过期。

    毫秒*秒*分钟*小时*天*周*月

    还请注意,我必须使用 parseInt,因为 getTime 函数没有返回整数。

    当您拥有该数字时,还值得对 timeToAdd 进行硬编码以提高代码效率。

    var timeToAdd = 1000 * 60 * 60 * 24 * 7 * 4 * 6;
    var date = new Date();
    var expiryTime = parseInt(date.getTime()) + timeToAdd;
    date.setTime(expiryTime);
    var utcTime = date.toUTCString();
    document.cookie = "YOUR_COOKIE=yes; expires=" + utcTime + ";";
    

    【讨论】:

    • 感谢“document.cookie”的完整示例以及所有这些!不知道为什么你有数学让它在 24 周内到期。您还可以内联和简化所有日期代码,因此您只有一行代码:document.cookie = "COOKIE_NAME=yes; expires=" + new Date(86400000 + Date.now()).toUTCString() + ';'
    【解决方案3】:

    这是我在项目中所做的,我觉得更容易理解。只需根据你想在当前时间上加多少天来更改最后一个数字,timestamp

    const timestamp = new Date().getTime(); // current time
    const exp = timestamp + (60 * 60 * 24 * 1000 * 7)
    

    60 分钟 * 60 秒 * 24 小时 * 1000(毫秒)* 7 天

    或者你可以简单地使用86400000

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-11-02
      • 2019-03-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-03-18
      • 2011-08-30
      • 1970-01-01
      相关资源
      最近更新 更多