【问题标题】:How to put timestamps in local storage?如何将时间戳放在本地存储中?
【发布时间】:2021-03-22 08:47:36
【问题描述】:

您好,我正在创建一个 chrome 扩展程序,我正在尝试将访问 url 后检索到的所有时间戳放入本地存储中的数组中,但我在这样做时遇到了麻烦。它继续更新时间戳,我希望我所有的时间戳保持不变。但是它似乎没有保存任何东西,我一直收到这个错误。有谁知道如何解决这个错误或可能导致它的原因。

function addRow() {

    //SavaDataToLocalStorage(data);
    
        //putting dates in array-- works somewhat
            
            
            //var myDate = data[data.length-1];
            //data.length = 0;
    
    const bg = chrome.extension.getBackgroundPage()    
    Object.keys(bg.get).forEach(function (url) {
    
    
        // Append product to the table
            var data = [];
            data = JSON.parse(localStorage.getItem('session')) || [];
            // Push the new data (whether it be an object or anything else) onto the array
            data.push( Date.now() );
            localStorage.setItem('session', JSON.stringify(data));
            myDate = data;
        
            //protocol
            var arr = url.split("/");
            var protocoll = arr[0] + "//" + arr[2];
        
            //inputed data --
            browser= "Google Chrome"; 
            protocol = protocoll;
            downloads = "example";
            description = "example";
            //use data or myDate as time
            time = myDate;
            //have as Date.now()
                
        
        //will put dates in array and replace it and have var as myDate
    // add new row to the table
                  //1 = in the top 
                  //table.rows.length = the end
                  //table.rows.length/2+1 = the center 

            var newRow = table.insertRow(0);
            
            //console.log(table.rows.length) gives length of table
                  
                  // add cells to the row
                  var browserCell = newRow.insertCell(0);
                  var timeCell = newRow.insertCell(1);
                  var urlCell = newRow.insertCell(2);
                  var protocolCell = newRow.insertCell(3);
                  var downloadsCell = newRow.insertCell(4);
                  var descCell = newRow.insertCell(5);
                  
                  // add the data to the cells
                  
                  urlCell.innerHTML = `${url}`; 
                  timeCell.innerHTML = time;
                    browserCell.innerHTML = browser;
                    descCell.innerHTML = description;
                    protocolCell.innerHTML = protocol;
                    downloadsCell.innerHTML = downloads;
                  console.log("add row works");
     }) 
            }
 

【问题讨论】:

  • 如果你 console.log 的话,localStorage.getItem('session') 是什么?您能否向我们展示更多背景信息?您的错误图像反映了在 forEach 调用中正在进行解析
  • @Taplar 我添加了更多上下文并修复了错字。它在函数 addrow() 中。我仍然收到同样的错误我的“会话”中目前没有任何内容。

标签: javascript json google-chrome-extension timestamp local-storage


【解决方案1】:

修正第 5 行日期的错字>>>数据

 var data = [];
    data = JSON.parse(localStorage.getItem('session')) || [];
    // Push the new data (whether it be an object or anything else) onto the array
    data.push( Date.now() );
    localStorage.setItem('session', JSON.stringify(data));
    myDate = data;

    //inputed data
    time = myDate;

【讨论】:

    猜你喜欢
    • 2021-03-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-30
    • 2011-02-06
    • 2018-10-06
    • 2019-09-18
    • 2021-04-17
    相关资源
    最近更新 更多