如何在更改后使localStorage无效

时间:2017-04-25 13:58:23

标签: javascript

我有一小段代码,允许我在我对其格式进行重大更改之前检查用户是否设置了localStorage。

var sw =
{
  storage: {
    // change this date when you create breaking changes in local storage
    // any local storage set before this time will be invalidated & set again
    lastBreakingUpdateTime: new Date(2017, 4, 24),

    local: {
      set: function(key, value) {
        try {
          window.localStorage.setItem(key.toString(), value.toString());
          return true;
        }
        catch(e) {
          return false;
        }
      },

      get: function(key) {
        var value = window.localStorage.getItem(key.toString());

        if (value === 'true')
          return true;
        if (value === 'false')
          return false;

        // isNan returns false for empty string
        // empty string is considered 0 by isNaN, but NaN by parseInt :)
        if (isNaN(value) || value === '')
          return value;

        // return value converted to number
        return +value;
      },

      markAsSetNow: function() {
        sw.storage.local.set('timeWhenSet', new Date());
      },

      isOutdatedOrNotSet: function() {
        var lastSetTime = sw.storage.local.get('timeWhenSet');
        if (!lastSetTime ||  Date.parse(lastSetTime) <= sw.storage.lastBreakingUpdateTime)
          return true;

        return false;
      }
    }
  }
}

此代码的问题在于,javascript中的Date.Parse在浏览器中是不可靠的 - 每个浏览器都有不同的实现。我需要修改此代码,以便它可以在每个主要浏览器中可靠地运行。

2 个答案:

答案 0 :(得分:1)

使用ms的时间比较日期以检查缓存:

const ms = new Date().getTime();

cross-browser

答案 1 :(得分:0)

最后我决定使用格式版本号,就像ayinloya在评论中建议的那样。如果有人想在他们的应用中使用,这是我完整的本地存储处理代码。

var appName =
{
  storage: {
    // increment this when you create breaking changes in local storage format
    // versions must start from 1, version 0 is invalid
    formatVersion: 1,

    // type can be 'localStorage' or 'sessionStorage'
    available: function(type) {
      try {
        var storage = window[type],
            x = '__storage_test__';
        storage.setItem(x, x);
        storage.removeItem(x);
        return true;
      }
      catch(e) {
        return false;
      }
    },

    local: {
      // Use this function over window.localStorage.setItem() because
      // localStorage.setItem() or sessionStorage.setItem() may throw
      // an exception if the storage is full.
      // in Mobile Safari (since iOS 5) it always throws when the user
      // enters private mode (Safari sets quota to 0 bytes in private mode,
      // contrary to other browsers, which allow storage in private mode,
      // using separate data containers).
      set: function(key, value) {
        try {
          window.localStorage.setItem(key.toString(), value.toString());
          return true;
        }
        catch(e) {
          return false;
        }
      },

      get: function(key) {
        var value = window.localStorage.getItem(key.toString());

        if (value === 'true')
          return true;
        if (value === 'false')
          return false;

        // isNan returns false for empty string
        // empty string is considered 0 by isNaN, but NaN by parseInt :)
        if (isNaN(value) || value === '')
          return value;

        // return value converted to number
        return +value;
      },

      setFormatVersion: function() {
        appName.storage.local.set('formatVersion', appName.storage.formatVersion);
      },

      isOutdatedOrNotSet: function() {
        var version = appName.storage.local.get('formatVersion');
        if (!version || version < appName.storage.formatVersion)
          return true;

        return false;
      }
    }
  }
}