jQuery Cookie 不保存值

jQuery Cookie doesn't save value

我有 jQuery Cookie 插件并希望我设置一个 cookie(过期)以在 x 次综合浏览量后执行脚本,但 cookie 没有 return 综合浏览量值但 return NaN .

$(document).ready(function () {
    var visited = $.cookie('visited'); // visited = 0
    if (visited == 3) {
        execute script
    } 
    else {
        visited++;// increase counter of visits

        // set new cookie value to match visits
        var date = new Date();
        date.setTime(date.getTime() + (10 * 1000));
        $.cookie('visited', visited, {expires: date});

        return false;
    }
});

我的脚本有什么问题?

试试这个:

$(document).ready(function () {
var visited = 0;
if ($.cookie('visited')) {//test if cookie exist 
  visited = $.cookie('visited');
}

    if (visited == 3) {
       //
    } 
    else {
        visited++;// increase counter of visits

        // set new cookie value to match visits
        var date = new Date();
        date.setTime(date.getTime() + (10 * 1000));
        $.cookie('visited', visited, {expires: 1});

        return false;
    }
});

https://jsfiddle.net/dfL94kjh/