gpt4 book ai didi

javascript - JQuery Cookie 1 分钟后过期

转载 作者:行者123 更新时间:2023-12-02 16:29:59 25 4
gpt4 key购买 nike

有谁知道如何设置 cookie 在 1 分钟后过期?我在代码中设置了 cookie 过期,但它似乎不起作用......任何人都可以帮助我解决这个问题吗?最好编写完整的代码或 Fiddle我,因为我不知道代码,抱歉。预先感谢您的帮助。这是我的 fiddle :http://jsfiddle.net/Garfrey/3Lzytvqy/4/

function showHide(setCookie) {
var shID = $(this).data("showhide")
, $shEl = $("#" + shID)
, $showHide = $("#" + shID + '-show')
;

if ($shEl.is(":hidden")) {
if (setCookie !== false) {
jQuery.cookie("showhide-" + shID, 1);
}
$showHide.hide();
$shEl.show();
} else {
if (setCookie !== false) {
jQuery.cookie("showhide-" + shID, 0);
}
$showHide.show();
$shEl.hide();
}
}

jQuery(document).ready(function () {
$("#example-show").on("click", showHide);
if (jQuery.cookie("showhide-" + "example") == '1') {
showHide.call($("#example-show").get(0), false);
}
});

var date = new Date();
var minutes = 1;
date.setTime(date.getTime() + (minutes * 60 * 1000));
$.cookie("example", "foo", { expires: date });

最佳答案

这是代码的简化版本:
我不知道预期的行为,因此这就是我能提供的帮助。

我确信如果您跟踪下面的代码,您可以轻松地将其与您的代码集成。
确保包含 jquery cookie js。

$('div#set_cookie').click(function(){
var date = new Date();
date.setTime(date.getTime() + (5 * 1000)); // 5 seconds
$.cookie('test', 1, { expires: date });

$('div#log').html('<b>cookie has been set! expires : '+date+'</b>');
});

$('div#check_cookie').click(function(){
var cookieValue = $.cookie("test");
if (cookieValue){
$('div#log').html('valid cookie');
}else{
$('div#log').html('expired cookie');
}

});

<强> jsfiddle

关于javascript - JQuery Cookie 1 分钟后过期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28391314/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com