0

I'm trying to save info about the checkboxes (if they are checked or not) in a cookie (jquery-cookie plugin). The problem is that the plugin doesn't allow anything less than 1 day to set in the "expires" line.

$(document).ready(function(){
    $(".ing_check").each(function(){
        var name = $(this).attr('name');
        if($.cookie(name) && $.cookie(name) == "true"){
            $(this).prop('checked', $.cookie(name));
        }
    });
    $(".ing_check").change(function(){
        var date = new Date();
        date.setTime(date.getTime()+(3*1000));

        var name = $(this).attr('name');
        $.cookie(name, $(this).prop('checked')), {
            path: '/',
            expires: date // plugin allows values like 365 or 2 - for days
        }
    })
});

I'm trying to expire the cookie after 3 seconds, but failed to do so. Am I doing something wrong? Is there a way to do it correctly?

Astw41
  • 394
  • 3
  • 12
  • Does this answer your question? [jQuery cookie expiry time](https://stackoverflow.com/questions/33232270/jquery-cookie-expiry-time) - "expires option accepts either a number or a date object.", you can also reference: [how-can-i-set-a-cookie-with-expire-time](https://stackoverflow.com/questions/13154552/how-can-i-set-a-cookie-with-expire-time) – Ryan Wilson Sep 06 '22 at 21:40
  • @RyanWilson No, it doesn't. As you can see in the post I've already done that. – Astw41 Sep 06 '22 at 21:50
  • Luckily you don't need to user jquery cookies. There are a ton of libraries out there that simplify cookie creation. Here is a tiny one that gives you access to localstorage, sessions and cookies; https://github.com/fend25/strg.js – imvain2 Sep 06 '22 at 22:01
  • @imvain2 I'd rather stick to jquery-cookies. Everything is working correctly but the timer. Is there a way to solve this problem? – Astw41 Sep 06 '22 at 22:07
  • I see that you mentioned 3 seconds, what is the purpose of the cookie if you are only needing it for 3 seconds? Can't you just use a variable to store the current state? – imvain2 Sep 07 '22 at 23:12
  • @imvain2 I'm not going to wait for 3 hours 10 times to see if the script works. Why wouldn't I set it to 3 seconds just to test it? Anyway, I solved the problem already. – Astw41 Sep 08 '22 at 09:55

0 Answers0