Whitewolf3131
Whitewolf3131

Reputation: 79

Cannot read property 'parentNode' of null. Error i get in console

 var button1 = document.getElementById("start");
var button2 = document.getElementById("stop");
var timegraph = document.getElementById("ceas");
var lapButton = document.getElementById("lap");
var lame = document.getElementById("test");
var time = 0;
var ResetStart = 0;
var myInterval;
var body = document.getElementById("body");
var action = document.getElementById("lappara");
function Start() {
    if (ResetStart == 0) {
        ResetStart = 1;
        Running();
        button1.innerHTML = "Pause";
    }
    else {
        ResetStart = 0;
        button1.innerHTML = "Resume";
        clearInterval(myInterval);
    }
}
function Reset() {
    time = 0;
    ResetStart = 0;
    button1.innerHTML = "Start";
    timegraph.innerHTML = "00:00:00:00";
    clearInterval(myInterval);
    var aux = action.parentNode;
    aux.removeChild(action);
}
function OnGoing() {
    time++;
    var hours = Math.floor(time / 100 / 60 / 60);
    var minutes = Math.floor(time / 100 / 60 % 60);
    var seconds = Math.floor(time / 100 % 60);
    var hundreds = Math.floor(time / 10 % 10);
    var thousands = time % 10;
    if (hours < 10) {
        hours = "0" + hours;
    }
    if (minutes < 10) {
        minutes = "0" + minutes;
    }
    if (seconds < 10) {
        seconds = "0" + seconds;
    }
    timegraph.innerHTML = hours + ":" + minutes + ":" + seconds + ":" + hundreds + thousands;
}
function Running() {
    if (ResetStart == 1) {
        myInterval = setInterval(OnGoing, 10);
    }
    else {
        timegraph.innerHTML = "00:00:00:00";
    }
}
function Lap() {
    var n = document.createElement("p");
    n.setAttribute("id", "lappara");
    var text = document.createTextNode(timegraph.innerHTML);
    n.appendChild(text);
    body.insertBefore(n, action);
}

Whenever i call the Reset() function i get the error above. This code is supposed to be a stopwatch and the Lap() function just creates laps. I looked up some solutions but none seemed to work. Please help me if you can

Upvotes: 0

Views: 1674

Answers (2)

Dmytro
Dmytro

Reputation: 343

I think you call func Reset() before create element with id lappara by lap() func or call Reset() function several times.

Upvotes: 0

Ajin
Ajin

Reputation: 148

I assume you can call Reset() once but next time you call it the error appears. This is because of aux.removeChild(action). You delete action and then you try to get it's parentNode - that's where the problem is.

Upvotes: 1

Related Questions