JQuery Cookie 在 1 分钟后过期

JQuery Cookie expire after 1 minute

有人知道我如何设置 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