Javascript 将对象推入数组CSS | JS | HTML时出现问题

Javascript 将对象推入数组CSS | JS | HTML时出现问题,javascript,html,css,arrays,Javascript,Html,Css,Arrays,我正在创建一个模块,其中信息(考试信息)存储在一个数组中,并使用localstorage传递给另一个js。我遇到的问题是,即使我正在使用.push,以前的考试也总是被覆盖。所有变量都在别处定义。谢谢你的帮助。代码如下: recentexamdb = [] document.getElementById("button1").onclick = function () { window.scrollTo(0, 0); document.getElementById("maindiv

我正在创建一个模块,其中信息(考试信息)存储在一个数组中,并使用localstorage传递给另一个js。我遇到的问题是,即使我正在使用.push,以前的考试也总是被覆盖。所有变量都在别处定义。谢谢你的帮助。代码如下:

recentexamdb = []
document.getElementById("button1").onclick = function () {
    window.scrollTo(0, 0);
    document.getElementById("maindiv").style.visibility = "hidden";
    document.getElementById("buttondiv").style.visibility = "hidden";
    document.getElementById("countdown").style.visibility= "visible";
    var yearinput = document.getElementById("yearinput").value;
    var minuteinput = document.getElementById("minuteinput").value;
    var hourinput = document.getElementById("hourinput").value;
    var secondinput = document.getElementById("secondinput").value;
    var twentyminutes = new Audio('20mins.mp3');
    var tenminutes = new Audio('10mins.mp3');
    var fiveminutes = new Audio('5mins.mp3');
    var timesup = new Audio('timesup.mp3');
    var firstalert = document.getElementById("firstalertinput").value;
    var secondalert = document.getElementById("secondalertinput").value;
    var thirdalert = document.getElementById("thirdalertinput").value;
    var readingalert = document.getElementById("readingalertinput").value;
    var timetotal = (Number(hourinput)*60) + Number(minuteinput)
    enddate = (new Date().getTime()) + (minuteinput*60000) +(secondinput*1000) + (hourinput*3600000);
    var examtypeinput = document.getElementById("examtypeinput").value;
    var hours=0;
    var minutes=0;
    var seconds=0;
    var countdown = document.getElementById("countdown");
    var examtype = document.getElementById("examtype");

 setInterval(function () {
        var currentdate = new Date().getTime();
        var secondsleft = (enddate - currentdate) / 1000;
        // do some time calculations
        hours = parseInt(secondsleft / 3600);
        secondsleft = secondsleft % 3600;
        minutes = parseInt(secondsleft / 60);
        seconds = parseInt(secondsleft % 60);
        if (hours == 0 && minutes == 0 && seconds == 0) {
          document.getElementById("countdowndiv").style.fontFamily = "Helvetica Neue, Arial, sans-serif"
          document.getElementById("countdowndiv").style.fontSize = "100%"
          countdown.innerHTML = "Time is up! Pens down and wait for further instruction"
          examtype.innerHTML = "If you continue to write, marks may be deducted"
          var readingtimenow = "No reading time selected"
          var timetotalnow = ((hours * 60) + minutes)
          var examinfoobject = {
            year: yearinput,
            examtime: timetotalnow,
            timeofexam: Date(),
            examtype: examtypeinput,
            readingtime: readingtimenow
          }
          recentexamdb.push(examinfoobject)
          console.log(recentexamdb)
          localStorage.setItem("storedexamdb", JSON.stringify(recentexamdb))
      }
}, 1000);
}

更正JS特有的语法(感谢您指出) 首先从本地存储中的现有值初始化“recentexamdb”,否则它将始终是一个新数组&早期保存的值可能会丢失

recentexamdb=JSON.parse(localStorage.getItem("storedexamdb")); // read  already saved values
 recentexamdb.push(examinfoobject);
 localStorage.setItem("storedexamdb",JSON.stringify(recentexamdb));
在下一页,您可以
希望这有帮助。

由于aarati的回答而修复。当我设置localstorage clickcounter并仅在clickcounter>1时使用已保存的值时,它就起作用了。请提供完整的相关代码。您是否已将recentexamdb声明为数组?添加了其余的代码?如果您创建了一个fiddle,请回答为什么?很遗憾,这似乎不起作用。我在检查对象之后添加了它,并在项目开始时声明了变量。我得到这个错误:timermodule.js:319 uncaughttypeerror:无法读取null的属性“push”。第319行位于recentexamdb.push(检查对象)