如何在特定时间在 jquery 中的 cookie、会话或本地存储中存储和恢复数组

how to store and restore an array in cookie, session or local storage in jquery for specific time

如何将数组的内容存储在 cookie、会话或本地存储中,然后再次恢复或推送到数组中以正常使用。应该在特定的时间设置,过了那个时间就不会恢复数值了。

编辑 我尝试了这个,因为 cookie 需要本地存储和会话存储以及数组

var date = new Date();
 var minutes = 30;
 date.setTime(date.getTime() + (minutes * 60 * 1000));
 $.cookie("example", "foo", { expires: date });

对于 cookie:

 var date = new Date();
 var cars = ["Saab", "Volvo", "BMW"]; 
 date.setTime(date.getTime() + (30 * 60 * 1000)); //Expires in 30 minutes
 $.cookie("cookieName", cars, { expires: date });
 alert( $.cookie("cookieName") );

对于本地存储:

var cars = ["Saab", "Volvo", "BMW"]; 
localStorage.setItem( 'name', cars );
date.setTime(date.getTime() + (30 * 60 * 1000));
var date = new Date();
localStorage.setItem( 'expiretime', date );
if(localStorage.getItem( 'expiretime' ) > Date()) {
    alert(localStorage.getItem( 'name' ));
}

会话:

var cars = ["Saab", "Volvo", "BMW"]; 
$.session.set("name", cars);
date.setTime(date.getTime() + (30 * 60 * 1000));
var date = new Date();
$.session.set("expiretime", date);
if($.session.get( 'expiretime' ) > Date()) {
    alert($.session.get( 'name' ));
}