简体   繁体   中英

addEventListener automatically triggers and keep continuing

I am using a click addEventListener to execute a function. But it seems to automatically trigger when the page loads and keep on continuing to trigger. How can I make the function work only when the button is clicked?

I am looking for a pure javascript solution (no jquery)

document.getElementById("myBtn").addEventListener("click", function (){
    loadMesh(vertics);
    console.log("triggered!! :(");
});

console:

triggered!! :( triggered!! :( triggered!! :( triggered!! :( .... .

Your current code will only run when you click button with id myBtn .

It seems like you are manually calling your click() event attached to your button when the page loads. Remove the manual call.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM