Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/86.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 将cookie设置为在一天后过期_Javascript_Jquery_Cookies - Fatal编程技术网

Javascript 将cookie设置为在一天后过期

Javascript 将cookie设置为在一天后过期,javascript,jquery,cookies,Javascript,Jquery,Cookies,我使用了从git获得的代码。它的基本设置是设置cookie,以便仅在第一次访问站点时显示弹出窗口。但是,我希望它只设置为24小时。因此,如果有人在一两天后回到该网站,它将再次显示 (function ($) { 'use strict'; $.fn.firstVisitPopup = function (settings) { var $body = $('body'); var $dialog = $(this); var

我使用了从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);
(函数($){
"严格使用",;
$.fn.firstvisitpoup=函数(设置){
变量$body=$('body');
变量$dialog=$(此);
var$停电;
var setCookie=函数(名称、值){
变量日期=新日期(),
expires='expires=';
date.setTime(date.getTime()+31536000000);
expires+=date.togmString();
document.cookie=name+'='+value+';“+expires+';path=/”;
}
var getCookie=函数(名称){
var allCookies=document.cookie.split(“;”),
cookieCounter=0,
currentCookie='';
for(cookieCounter=0;cookieCounter
更改:

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


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

这是我在项目中所做的,我觉得更容易理解。只需根据您要添加到当前时间的天数更改最后一个数字,
timestamp

const timestamp = new Date().getTime(); // current time
const exp = timestamp + (60 * 60 * 24 * 1000 * 7)
60分钟*60秒*24小时*1000(毫秒)*7天


或者,您可以简单地使用
86400000

日期设置时间函数期望以毫秒为单位的时间。 在我下面的例子中,cookie大约在6个月后过期

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

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

当你有这个数字时,硬编码要添加的时间也是值得的,这样可以提高代码的效率

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 + ";";

因此,将设置时间更改为一天31536000000是以毫秒为单位的一年,因此将其更改为以毫秒为单位的一天
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 + ";";