dimanche 2 mai 2021

javascript event listener not accepting an object's method [duplicate]

So I was trying to create an audio player using javascript when I stumbled upon something, the following code works:

var player = new Audio('sound.mp3');
function playSound(){
player.play();
}
document.querySelector('button').addEventListener('click', playSound);

but this one doesn't:

var player = new Audio('sound.mp3');
document.querySelector('button').addEventListener('click', player.play);

and I couldn't understand why.




Aucun commentaire:

Enregistrer un commentaire