admin管理员组文章数量:1404587
I am making a browser extension and I want to modify some things on the page but I run into this problem.
Then event listener gets triggered 'plug-stats' is not yet created by script on the page I can't control and gives me undefined so I tried to read it a little bit later but then I run into a problem.
First console log will give me a target as expected but I can't do anything with it yet because 'plug-stats' were not yet created. Second console log gives me null.
Why is this happening and any ideas on how can I fix it?
let perk = document.querySelectorAll('.ItemPerksList-m_plug-O8be3')
perk.forEach(element => {
element.addEventListener('click', add_perk_description);
});
function add_perk_description(event){
console.log(event.currentTarget)
setTimeout(() => {
console.log(event.currentTarget)
let html = event.currentTarget.getElementsByClassName('plug-stats')[0]
}, 1)
}
I am making a browser extension and I want to modify some things on the page but I run into this problem.
Then event listener gets triggered 'plug-stats' is not yet created by script on the page I can't control and gives me undefined so I tried to read it a little bit later but then I run into a problem.
First console log will give me a target as expected but I can't do anything with it yet because 'plug-stats' were not yet created. Second console log gives me null.
Why is this happening and any ideas on how can I fix it?
let perk = document.querySelectorAll('.ItemPerksList-m_plug-O8be3')
perk.forEach(element => {
element.addEventListener('click', add_perk_description);
});
function add_perk_description(event){
console.log(event.currentTarget)
setTimeout(() => {
console.log(event.currentTarget)
let html = event.currentTarget.getElementsByClassName('plug-stats')[0]
}, 1)
}
Share
Improve this question
edited Jun 1, 2021 at 16:08
XB BX
asked Jun 1, 2021 at 13:42
XB BXXB BX
888 bronze badges
3 Answers
Reset to default 7This is expected behaviour, the documentation for Event.currentTarget
actually mentions this:
The value of event.currentTarget is only available while the event is being handled. If you console.log() the event object, storing it in a variable, and then look for the currentTarget key in the console, its value will be null.
The while the event is being handled part should essentially be read as synchronously within the event hanlder function as I understand.
https://developer.mozilla/en-US/docs/Web/API/Event/currentTarget
You could still keep a reference to the current target for instance by synchronously assigning it to a local variable - and then referring to this local variable in the setTimeout()
callback function.
function add_perk_description(event){
console.log(event.currentTarget)
let currentTarget = event.currentTarget
setTimeout(() => {
console.log(currentTarget)
let html = currentTarget.getElementsByClassName('plug-stats')[0]
}
}, 1)
}
try this code pass the event as parameter to settimeout-
function add_perk_description(e){
console.log(e.target)
setTimeout(function(event) {
console.log(event.target)
},500, e);
}
Second Console gives you a NULL because event.currentTarget return a valid value at the time of being handled but callback runs after a 1 milliSeconds that's the reason it shows Null.
Try out this
let perk = document.querySelectorAll('.ItemPerksList-m_plug-O8be3')
perk.forEach(element => {
element.addEventListener('click', e => {
add_perk_description(e)
})
})
function add_perk_description(event){
console.log(event.currentTarget);
**let {currentTarget}=e;**
setTimeout(() => {
console.log(currentTarget);
let html = e.currentTarget.getElementsByClassName('plug-stats')[0]
}
}, 1)
}
本文标签: javascriptEvent listener target is null after delay with set time outStack Overflow
版权声明:本文标题:javascript - Event listener target is null after delay with set time out - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744838108a2627755.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论