0

我试图让cookie在最后一次点击后记住按钮点击约X分钟,即使重新加载页面,然后在点击X分钟后再次启用它,但我做不到,有没有人能帮我做到这一点?这是设置限制代码,我已经完成了,我只需要为我设置 cookie。

(function(){
    var click_counter = 0;
    jQuery('.button').on('click', function(event){
        event.preventDefault();
        var el = jQuery(this);
        click_counter += 1;
        if (!el.hasClass('inactive')){
            // should be activated
        };
        if (click_counter >= 6){
            // deactivate
            el.addClass('inactive');
             alert('Message');
        };
    });
})();

谢谢你

4

1 回答 1

0

我想我明白了,我会做这样的事情:试试这个:

function writeCookie (key, value, mins) {
    var date = new Date();

    // Get unix milliseconds at current time plus number of hours
    date.setTime(+ date + (mins * 3600000)); //60 * 60 * 1000

    window.document.cookie = key + "=" + value + "; expires=" + date.toGMTString() + "; path=/";

    return value;
};

用法:

<script>
writeCookie ("myCookie", "clicked", 2);
</script>
//for 2 mins

我们要检查页面何时加载:

<script>
$c_value = document.cookie;
if ($c_value == "clicked") {
// whatever you code to deactivate is

}
</script>
于 2013-10-19T22:31:13.033 回答