How to expire a cookie in 30 minutes using jQuery?

后端 未结 3 752
灰色年华
灰色年华 2020-11-27 10:55

How to Expire a Cookie in 30 min ? I am using a jQuery cookie. I am able to do something like this.

$.cookie(\"example\", \"foo\", { expires: 1 });
         


        
3条回答
  •  难免孤独
    2020-11-27 11:01

    If you're using jQuery Cookie (https://plugins.jquery.com/cookie/), you can use decimal point or fractions.

    As one day is 1, one minute would be 1 / 1440 (there's 1440 minutes in a day).

    So 30 minutes is 30 / 1440 = 0.02083333.

    Final code:

    $.cookie("example", "foo", { expires: 30 / 1440, path: '/' });
    

    I've added path: '/' so that you don't forget that the cookie is set on the current path. If you're on /my-directory/ the cookie is only set for this very directory.

提交回复
热议问题