I've noticed a difference between function
and function()
for addEventListener's callback.
Which isn't a problem till I tried passing a parameter. Basically,
element.addEventListener("hover", logInput, false );
function logInput(){
console.log('registered!');
}
works as intended, but adding parenthesis will cause it log immediately, without continual response to the event trigger:
element.addEventListener("hover", logInput(), false );
function logInput(){
console.log('registered!');
}
Why is this? And how can I get it to work while passing a parameter such as:
element.addEventListener("hover", logOnInput(this), false );
function logOnInput(triggeredElement){
console.log(triggeredElement);
}
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…