【发布时间】: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">✖</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