Why is an error given when I remove the child node? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Why is an error given when I remove the child node?

https://code.sololearn.com/WxhFYWzuJx0E/?ref=app 1)When the code runs, a ball goes across the screen. 2)Once the ball goes past the finish line I want the canvas element to be removed from the screen. Both of these appear to happen, however, an error is given when the canvas element is removed and I’m not sure why. It says the canvas element is not a child element of document.body, but I know that it is because I appended it. The element disappears despite throwing an error. Any help is appreciated

27th Jan 2019, 3:25 AM
Thomas Czernek
Thomas Czernek - avatar
2 Answers
+ 1
your program remove the canvas element and then tries to do it again every 5 ms. use clearInterval () to stop calling the animate function: win(){ if(this.xPos > canvas.width * 0.80){ document.body.removeChild(canvas); clearInterval(t); } } let t=setInterval(animate, 5);
27th Jan 2019, 6:10 AM
Игорь Яковенко
Игорь Яковенко - avatar
0
Try this instead, to refer its parent and then remove element itself. if(canvas.parentNode) canvas.parentNode.removeChild(canvas);
27th Jan 2019, 6:18 AM
Calviղ
Calviղ - avatar