将对象推入数组CSS的问题JS | HTML

时间:2016-07-04 06:53:07

标签: javascript html css arrays

创建一个模块,其中信息(考试信息)存储在一个数组中,并使用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);
}

2 个答案:

答案 0 :(得分:0)

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

recentexamdb=JSON.parse(localStorage.getItem("storedexamdb")); // read  already saved values
 recentexamdb.push(examinfoobject);
 localStorage.setItem("storedexamdb",JSON.stringify(recentexamdb));

在下一页,你可以 希望这会有所帮助。

答案 1 :(得分:0)

由于aarati的回答而修复。当我设置localstorage clickcounter并且当clickcounter为>时仅使用已保存的值