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
2 Antworten
+ 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);
0
Try this instead, to refer its parent and then remove element itself.
if(canvas.parentNode) canvas.parentNode.removeChild(canvas);