Why doesn’t Event exist inside the lower block?

Asked

Viewed 51 times

3

I’m having trouble accessing the event.target within the function setTimeout in the code below.

const botao = document.querySelector('button');

botao.addEventListener('click', () => {
  console.log(event.target);
  setTimeout(function(event) {
        console.log(event.target)        
    }, 500);

});

  • Try this.event.target

  • Still returns as cannot get target of Undefined

  • Pq the Event within the setTimeout doesn’t mean anything there, has no connection to the button Event.

  • addeventlistener('click', (Event) => {your code...}) Failed to set as callback parameter Event to use it.

2 answers

4

These are different events. The first is the click event, the second is the timeout; the second does not possess a target, only the first.

What you’re doing is what we call closure, where a function is defined based on the current scope of the definition.

In order for it to work the way you want it to, you must keep a reference to the target of the click event in a variable in the same context as you set the closure:

const botao = document.querySelector('button');

botao.addEventListener('click', event => {
  // Aqui, event refere-se ao evento do clique
  const target = event.target;
  
  setTimeout(function(event) {
      // Aqui, event refere-se ao evento do timeout
      console.log(target)        
  }, 500);
});
<button>Pressione-me</button>

See that target was defined as a reference to target click event and used within the closure.

2


The event is a global variable window and only gains properties within the Event Handler, like the addEventListener.

The setTimeout is a global function that does not assign to the variable event the event properties, logo, event isolated within the function of setTimeout has no value whatsoever.

But first we need to know that it is always important to pass on the function of Event Handler the first parameter, representing the triggered event:

botao.addEventListener('click', (event) => {
                                   ↑
                               parâmetro

This is because less recent versions of browsers (such as Firefox) do not have the variable event native, that is, the first console.log(event.target); would already return the error event is not defined, as explained in this other answer.

Therefore, when passing the event as a parameter of the addEventListener it becomes a variable within the function and will have value also within the function of the setTimeout:

const botao = document.querySelector('button');
botao.addEventListener('click', (event) => {
  console.log(event.target);
   setTimeout(function() {
     console.log(event.target)        
   }, 500);
});
<button>Clique</button>

On the other hand, if you do not pass the event as a parameter of addEventListener (which is not recommended, as explained above), you could submit the variable event for the function of setTimeout as follows, placing event after the time of 500:

const botao = document.querySelector('button');
botao.addEventListener('click', () => {
  console.log(event.target);
   setTimeout(function(event) {
     console.log(event.target)        
   }, 500, event);
});
<button>Clique</button>

  • I understand that it has to be passed as a function parameter to exist within the setTimeout, but even without passing, it is shown in the top row so because it is not shown within the setTimeout?

  • 1

    @Sam I don’t understand your answer console.log(and) within the setTimeout returns undefined, I mean, it’s not doing anything there

  • 1

    Rsrs.. I will correct

  • yes, in setTimeout is a function without rsrs parameters

Browser other questions tagged

You are not signed in. Login or sign up in order to post.