2017-04-25 40 views
0

我有一小段代碼,它允許我檢查用戶是否在本地格式發生重大更改之前設置了localStorage。如何在破壞更改後使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是不可靠的跨瀏覽器 - 每個瀏覽器都有不同的實現。我需要修改此代碼,以便它可以在每個主要瀏覽器中可靠地工作。在MS

+0

嘗試設置版本號,始終檢查瀏覽器版本是否與當前版本相同。 – ayinloya

+0

@ayinloya - 好主意,謝謝 –

+0

不用客氣;) – ayinloya

回答

1

使用時間的日期,以便進行比較檢查緩存:

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

這是cross-browser

+0

謝謝我不知道這件事。我除了它,但我需要等待10分鐘。 –

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; 
     } 
    } 
    } 
}