简体   繁体   English

本地存储无法按预期工作

[英]Local Storage Not Working As Expected

I built an app that I then built with PhoneGap Build.THe purpose is for it to run a code (starts at var Quotes once per day when the app is loaded). 我构建了一个应用程序,然后使用PhoneGap Build构建了该应用程序。运行该应用程序的目的是使其运行代码(加载该应用程序时每天从var Quotes开始)。

When debugging why it wasn't working I noticed that in console I was getting back my message "Local storage didn't work". 当调试为什么它不起作用时,我注意到在控制台中,我回想起“本地存储不起作用”的信息。 This means that my initial localstorage.getItem which is supposed to make sure the local storage can be read is returning null . 这意味着我最初localstorage.getItem确保可以读取本地存储的localstorage.getItem返回null So my code never gets executed. 所以我的代码永远不会执行。

What am I doing wrong? 我究竟做错了什么?

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

Initially, there will be no DateOpened item in local storage, so your code will follow the "did not work" branch, because getItem returns null for things that don't exist. 最初,本地存储中将没有DateOpened项,因此您的代码将遵循“未工作”分支,因为getItem对于不存在的内容返回null That branch never sets anything in DateOpened , so...you'll always follow that branch. 该分支永远不会在DateOpened设置任何DateOpened ,因此...您将始终遵循该分支。

The fix is not to skip over your code setting DateOpened if the device has local storage. 解决方法是,如果设备具有本地存储,则不要跳过代码设置DateOpened


There's also an unrelated problem: Your var str = tempd.getDay() + tempd.getMonth() + tempd.getFullYear() does not produce a string, it produces a number formed by adding those values together, since they're all numbers. 还有一个不相关的问题:你的var str = tempd.getDay() + tempd.getMonth() + tempd.getFullYear() 不会产生一个字符串,它产生通过这些值形成了一些共同的,因为他们是所有数字。 Your later localeCompare will fail because it's not a string. 您以后的localeCompare将失败,因为它不是字符串。 You also have the fields in the wrong order for a meaningful textual comparison — you need year first, then month, then day. 您还需要按照错误的顺序对字段进行有意义的文本比较-您需要先输入年份,然后是月份,然后是日期。


Here's a minimal fix, see comments: 这是一个最小的修复程序,请参阅注释:

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

I think this is help full for you. 我认为这对您有很大帮助。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM