'Why doesn't my code work outside the setTimeOut function?

Outside of the function, it works very well. But when I put it inside, the console says "cannot read properties of null (reading 'parentNode')". I just want to execute that code after one second.

setTimeout(function(){
        ocultar(verde, imagen);
        let imagen2 = document.getElementById(buscarCarta(levantadas[0]));
        ocultar(imagen2.parentNode.parentNode.lastChild, imagen2);
    }, 1000);


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source