简体   繁体   English

array.push 不工作 - 无法添加到数组

[英]array.push not working - unable to add to array

I'm making a speedcube timer, but can't add times to arrays.我正在制作一个 speedcube 计时器,但无法为 arrays 添加时间。

var solves = 0;
var times = [];
var solveCount = document.querySelector(“.nameofDiv”);
var solveTable = document.getElementById(“div”);
var timer = document.querySelector(“.timer”);


function getTime(){
    solves++;
    localStorage.setItem("solvesDone", JSON.parse(solves));
    solveCount.innerHTML = "Solves: " + solves;

    console.log("now doing times")

    var timeFinish = timer.innerHTML.slice(0, 2) + timer.innerHTML.slice(3, 5) + timer.innerHTML.slice(6, 8);
    var timeofSolve = timeFinish;
  
    alert(timeofSolve);
  
    times.push(timeofSolve);
  
    alert(times);
  
    localStorage.setItem("timesDone", JSON.parse(times));

    console.log("now appending");

    var div = document.createElement(div);
  
    div.classList.add("solveBar");
    solveTable.appendChild(div);

    console.log("finished func");
}

It stops when I do times.push .当我执行times.push时它会停止。

Full project here for context: https://speedcube-timer.coderguru.repl.co/完整的项目在这里: https://speedcube-timer.coderguru.repl.co/

Thanks in Advance提前致谢

your problem is your var declarations, it should be "var" not "Var"你的问题是你的 var 声明,它应该是“var”而不是“Var”

 var solves = 0; var times = [] var solveCount = 'solveCount_TODO'; var solveTable = 'solveTable_TODO'; function getTime() { solves++; //localStorage.setItem("solvesDone", JSON.parse(solves)); solveCount.innerHTML = "Solves: " + solves; console.log("now doing times") var timeFinish = "timeFinish_TODO"; var timeofSolve = timeFinish; alert(timeofSolve) times.push(timeofSolve); alert("Times: " + times); //localStorage.setItem("timesDone", JSON.parse(times)); console.log("now appending") var div = document.createElement(div); div.classList.add("solveBar"); //solveTable.appendChild(div); console.log("finished func") } getTime()

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

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