【问题标题】:Local Storage Not Working As Expected本地存储未按预期工作
【发布时间】:2016-03-07 16:13:18
【问题描述】:

我构建了一个应用程序,然后使用 PhoneGap Build 构建了它。目的是让它运行代码(在加载应用程序时每天从 var Quotes 开始一次)。

在调试它为什么不起作用时,我注意到在控制台中我收到了我的消息“本地存储不起作用”。这意味着我最初的localstorage.getItem 应该确保可以读取本地存储,它正在返回null。所以我的代码永远不会被执行。

我做错了什么?

function onDeviceReady() { //Do something when the app on device is loaded

var localVal = localStorage.getItem('DateOpened'); 

if(localVal  == null){ 

   console.log("LocalStorage did not work...")}

else
{
  var tempd = new Date(); //Get today's date
  var str = tempd.getDay() + tempd.getMonth() + tempd.getFullYear();
  if(localVal.localeCompare(str) == -1) 
              { 
                            var Quotes = [];
                            var ID = [];
                            var Tag = [];
                            var seen = [];

    localStorage.setItem('DateOpened',str);
    console.log("The App Ran, you can get a new fat tomorrow");
    console.log("Todays date:" + str);
  }
 }
}

【问题讨论】:

    标签: javascript cordova local-storage phonegap-build


    【解决方案1】:

    最初,本地存储中不会有 DateOpened 项,因此您的代码将遵循“无效”分支,因为 getItem 为不存在的事物返回 null。该分支永远不会在DateOpened 中设置任何内容,所以……您将始终遵循该分支。

    如果设备具有本地存储,则修复方法是不要跳过您的代码设置 DateOpened


    还有一个不相关的问题:您的var str = tempd.getDay() + tempd.getMonth() + tempd.getFullYear() 确实产生一个字符串,它产生一个通过添加这些值一起形成的数字,因为它们都是数字.您以后的 localeCompare 将失败,因为它不是字符串。为了进行有意义的文本比较,您也有错误的字段顺序 - 您需要先年份,然后是月份,然后是日期。


    这是一个最小的修复,请参阅 cmets:

    function onDeviceReady() {
    
        var tempd = new Date();
        // Note that by adding strings in there, we end up with a string instead of adding.
        // Note the order: Year first, then month, then day.
        // Also, since we display it, we put separators in and add 1 to month (since Jan = 0).
        var str = tempd.getFullYear() + "-" + (tempd.getMonth() + 1) + "-" + tempd.getDay();
        var localVal = localStorage.getItem('DateOpened');
    
        // If we have no stored value, or it's more than a day old by your definition,
        // do your stuff and store the new date
        if (localVal == null || localVal.localeCompare(str) < 0) {
            var Quotes = [];
            var ID = [];
            var Tag = [];
            var seen = [];
    
            localStorage.setItem('DateOpened', str);
            console.log("The App Ran, you can get a new fat tomorrow");
            console.log("Todays date:" + str);
        }
    }
    

    【讨论】:

    • 我怀疑由于从未设置过 localVar,它也会为空。谢谢 TJ,我会看看这是否有效。
    【解决方案2】:

    我认为这对你很有帮助。

    function onDeviceReady() { //Do something when the app on device is loaded
    
    var localVal = localStorage.getItem('DateOpened'); 
    
    if (typeof(DateOpened) == "undefined") 
    
       console.log("LocalStorage did not work...")}
    
    else
    {
      var tempd = new Date(); //Get today's date
      var str = tempd.getDay() + tempd.getMonth() + tempd.getFullYear();
      var allRecords=JSON.parse(localStorage.getItem("DateOpened"));     
      if(allRecords == -1) 
                  { 
                                var Quotes = [];
                                var ID = [];
                                var Tag = [];
                                var seen = [];
    
        localStorage.setItem('DateOpened',str);
        console.log("The App Ran, you can get a new fat tomorrow");
        console.log("Todays date:" + str);
      }
     }
    }

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-07-18
      • 1970-01-01
      • 2013-02-10
      • 2017-05-28
      • 2016-05-02
      • 2017-06-22
      • 1970-01-01
      • 2016-02-21
      相关资源
      最近更新 更多