将Cookie设置为在一天后过期 [英] Setting cookie to expire after one day
本文介绍了将Cookie设置为在一天后过期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我使用了我从git获得的代码.它的基本设置是将Cookie设置为仅在首次访问该网站时显示弹出窗口.但是,我希望它仅设置24小时.因此,如果有人在一两天内回到该站点,它将再次显示.
I have used this code I got from git. Its basically set up to set a cookie to display a popup on the first visit to the site only. However, I want it to only set it for 24 hours. So if someone comes back to the site in a day or two it will show again.
(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">✖</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);
推荐答案
更改:
date.setTime(date.getTime() + 31536000000);
收件人:
date.setDate(date.getDate() + 1);
这会将日期增加1天.旧代码增加了365天.
This adds 1 day to the date. The old code was adding 365 days.
这篇关于将Cookie设置为在一天后过期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文