0
Good morning, I’m trying to find out how to access a declared attribute in a setInterval/setTimeOut function in another function. With the little study I have on Javascript, a variable declared in local scope cannot be accessed in another local scope (a function accessing attributes of another) unless it is this.variavel
because it makes it "publish" and you instate an object of that variable in the other function obj = new NomeDaFuncao()
. Unfortunately I don’t know how to instantiate an object of a setInterval/setTimeOut function. Below is the example of what I’m trying to do.
setInterval(() =>
{
this.btn = document.querySelector(".remove-button");
},0);
function FazAlgo()
{
btnRemove = new setInterval();
console.log(btnRemove.btn.value);
}
Explain better what you want to achieve with this because the code is very confusing.
– bfavaretto
is a long story... pretend you only have a variable=10 there in setInterval kkkk
– Jefter Rocha
is why this button
.remove-button
is added dynamically, it is not in the html page yet, so I put this loop there to select it when it is generated '-'– Jefter Rocha
Use
this
only makes sense if you are working within an object / class definition. Otherwise, this will point to the window and you’ll end up creating a global variable - which may even solve your problem, but it’s a bad idea.– bfavaretto