addEventListener Executing Before Being Clicked

前端 未结 2 411
被撕碎了的回忆
被撕碎了的回忆 2021-01-28 01:18

I\'m Developing a basic Chrome extension centred around a video game. In order to create the menu I have used HTML and come up with:

    
        <         


        
2条回答
  •  深忆病人
    2021-01-28 01:47

    Don't call the startGame and openInfo functions as you do here:

    document.querySelector("#startButtonImage").addEventListener('click', startGame());
    document.querySelector("#infoButtonImage").addEventListener('click', openInfo());
    

    Instead do:

     document.querySelector("#startButtonImage").addEventListener('click', startGame);
     document.querySelector("#infoButtonImage").addEventListener('click', openInfo);
    

    This passes the function itself as a parameter to the addEventHandler function, rather than the return value of the function.

提交回复
热议问题