【问题标题】:Removing an item in HTML5 Local Storage after a period of time?一段时间后删除 HTML5 本地存储中的项目?
【发布时间】:2012-02-27 04:09:41
【问题描述】:

我目前正在开发一个简单的 HTML5 应用程序,我想知道是否可以在一段时间后删除 HTML5 本地存储中的项目,例如:24 小时后,删除该项目等。

我认为 JavaScript 中内置的 Date 对象可能是我需要的。

这可能吗?如果可以的话,一些代码示例会很好,谢谢!

【问题讨论】:

    标签: javascript html local-storage


    【解决方案1】:

    您可以将日期与数据一起存储

    //add data we are interested in tracking to an array
    var values = new Array();
    var oneday = new Date();
    oneday.setHours(oneday.getHours() + 24); //one day from now
    values.push("hello world");
    values.push(oneday);
    try {
        localStorage.setItem(0, values.join(";"));
    } 
    catch (e) { }
    
    //check if past expiration date
    var values = localStorage.getItem(0).split(";");
    if (values[1] < new Date()) {
        localStorage.removeItem(0);
    }
    

    【讨论】:

    • 我喜欢这个解决方案,但是我认为对于这个特定的示例应该注意,在比较之前需要将 values[1] 解析为 Date 对象。喜欢这个新的 Date(values[1])
    • 日期取自用户的操作系统,如果用户篡改了操作系统的日期设置,他将绕过这个
    【解决方案2】:

    使用此解决方案:

    (function () {
    
      var lastclear = localStorage.getItem('lastclear'),
          time_now  = (new Date()).getTime();
    
      // .getTime() returns milliseconds so 1000 * 60 * 60 * 24 = 24 days
      if ((time_now - lastclear) > 1000 * 60 * 60 * 24) {
    
        localStorage.clear();
    
        localStorage.setItem('lastclear', time_now);
      }
    
    })();
    

    【讨论】:

    • 谁需要留下来?!如果您在过去 X 次访问该站点,则会检查您的 localStorage,如果它更大,则将其删除。
    • 我认为你的意思是 1000 * 60 * 60 * 24 = 24 小时而不是 24 天
    【解决方案3】:

    如果您想这样做,我认为您基本上必须手动进行。例如,您可以将时间戳存储在您存储的每个值旁边的 localStorage 插槽中,然后以某个固定时间间隔(例如页面加载或 setTimeout 或其他时间)检查时间戳与当前时间。

    例子:

    //this function sets the value, and marks the timestamp
    function setNewVal(prop)
    {
        window.localStorage[prop] = Math.random();
        window.localStorage[prop+"timestamp"] = new Date();
    }
    
    //this function checks to see which ones need refreshing
    function someRucurringFunction()
    {
        //check each property in localStorage
        for (var prop in window.localStorage)
        {   //if the property name contains the string "timestamp"
            if (prop.indexOf("timestamp") != -1)
            {   //get date objects
                var timestamp = new Date(window.localStorage[prop]);
                var currentTime = new Date();
    
                //currently set to 30 days, 12 hours, 1 min, 1s (don't set to 0!)
                var maxAge =    (1000   *   1)  *//s
                                (60     *   1)  *//m
                                (60     *  12)  *//h
                                (24     *  30);  //d
    
                if ((currentTime - timestamp) > maxAge)
                {//if the property is too old (yes, this really does work!)
                    //get the string of the real property (this prop - "timestamp")
                    var propString = prop.replace("timestamp","");
                    //send it to some function that sets a new value
                    setNewVal(propString);
                }
            }
        }
    }
    //set the loop
    window.setInterval(someRucurringFunction,(1000*60*60);
    

    编辑:mrtsherman 的方法也完全有效。同样,您可以输入时间戳作为您可能使用 JSON.stringify/parse() 存储/检索的对象的属性。如果数组或对象非常大,或者你有很多,我可能会建议使用并行属性方法来提高效率。

    【讨论】:

    • localhost中存储和比较时应该使用(new Date).getTime()
    【解决方案4】:
    window.setInterval(function () {
            localStorage.setItem('nicwincount', 0);
    },8640000); //24 * 60mins * 60sec
    

    ps:nicwincount 是你之前设置的localstorage。 localStorage.setItem('feeds', Ext.encode(feeds));

    希望能帮到你。

    【讨论】:

    • 这不是假设设备在页面上停留 24 小时吗?不太可能! ;-)
    猜你喜欢
    • 1970-01-01
    • 2019-11-26
    • 2019-08-17
    • 2023-03-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多