Why does the event fire more than once?











up vote
-2
down vote

favorite












For the true boolean on eventListeners I read here:




once: A Boolean indicating that the listener should be invoked at most once after being added. If true, the listener would be automatically removed when invoked.




window.addEventListener("keydown", function(event){
switch(event.code){
case 'Digit1':
shoot('Ball1');
break;

case 'Digit2':
shoot('Ball2');
break;
}
}, true);


In my code the function shoot is called endless. Why?



And how can I fix it? The function shoot() may start only once.



As recommended I changed 'true' into '{once: true}'. It still call shoot() endless. Do you have an idea, why?



Thanks to @CertainPerformance.



If you should have the same problem be aware, that the internet explorer doesn't know event.code.










share|improve this question
























  • Maybe you add the event listener more than once?
    – yossico
    Nov 22 at 7:30










  • Up to now this is the only event listener in my code. If another one will be neccessary, I can't say at this moment.
    – NewbieXXL
    Nov 22 at 7:33















up vote
-2
down vote

favorite












For the true boolean on eventListeners I read here:




once: A Boolean indicating that the listener should be invoked at most once after being added. If true, the listener would be automatically removed when invoked.




window.addEventListener("keydown", function(event){
switch(event.code){
case 'Digit1':
shoot('Ball1');
break;

case 'Digit2':
shoot('Ball2');
break;
}
}, true);


In my code the function shoot is called endless. Why?



And how can I fix it? The function shoot() may start only once.



As recommended I changed 'true' into '{once: true}'. It still call shoot() endless. Do you have an idea, why?



Thanks to @CertainPerformance.



If you should have the same problem be aware, that the internet explorer doesn't know event.code.










share|improve this question
























  • Maybe you add the event listener more than once?
    – yossico
    Nov 22 at 7:30










  • Up to now this is the only event listener in my code. If another one will be neccessary, I can't say at this moment.
    – NewbieXXL
    Nov 22 at 7:33













up vote
-2
down vote

favorite









up vote
-2
down vote

favorite











For the true boolean on eventListeners I read here:




once: A Boolean indicating that the listener should be invoked at most once after being added. If true, the listener would be automatically removed when invoked.




window.addEventListener("keydown", function(event){
switch(event.code){
case 'Digit1':
shoot('Ball1');
break;

case 'Digit2':
shoot('Ball2');
break;
}
}, true);


In my code the function shoot is called endless. Why?



And how can I fix it? The function shoot() may start only once.



As recommended I changed 'true' into '{once: true}'. It still call shoot() endless. Do you have an idea, why?



Thanks to @CertainPerformance.



If you should have the same problem be aware, that the internet explorer doesn't know event.code.










share|improve this question















For the true boolean on eventListeners I read here:




once: A Boolean indicating that the listener should be invoked at most once after being added. If true, the listener would be automatically removed when invoked.




window.addEventListener("keydown", function(event){
switch(event.code){
case 'Digit1':
shoot('Ball1');
break;

case 'Digit2':
shoot('Ball2');
break;
}
}, true);


In my code the function shoot is called endless. Why?



And how can I fix it? The function shoot() may start only once.



As recommended I changed 'true' into '{once: true}'. It still call shoot() endless. Do you have an idea, why?



Thanks to @CertainPerformance.



If you should have the same problem be aware, that the internet explorer doesn't know event.code.







javascript addeventlistener






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 22 at 9:21

























asked Nov 22 at 7:26









NewbieXXL

397




397












  • Maybe you add the event listener more than once?
    – yossico
    Nov 22 at 7:30










  • Up to now this is the only event listener in my code. If another one will be neccessary, I can't say at this moment.
    – NewbieXXL
    Nov 22 at 7:33


















  • Maybe you add the event listener more than once?
    – yossico
    Nov 22 at 7:30










  • Up to now this is the only event listener in my code. If another one will be neccessary, I can't say at this moment.
    – NewbieXXL
    Nov 22 at 7:33
















Maybe you add the event listener more than once?
– yossico
Nov 22 at 7:30




Maybe you add the event listener more than once?
– yossico
Nov 22 at 7:30












Up to now this is the only event listener in my code. If another one will be neccessary, I can't say at this moment.
– NewbieXXL
Nov 22 at 7:33




Up to now this is the only event listener in my code. If another one will be neccessary, I can't say at this moment.
– NewbieXXL
Nov 22 at 7:33












2 Answers
2






active

oldest

votes

















up vote
3
down vote













The third parameter provided to addEventListener is either an options object, or useCapture, a boolean. Because you passed a boolean, the interpreter is considering you to have indicated that you want the function to trigger in the capturing phase, rather than the bubbling phase - which is not something you care about.



Pass an options object with a once property instead:



window.addEventListener(
"keydown",
function(event){
switch(event.code){
case 'Digit1':
shoot('Ball1');
break;

case 'Digit2':
shoot('Ball2');
break;
}

},
{ once: true }
);





window.addEventListener(
"keydown",
function(event){
console.log('function running');
},
{ once: true }
);





Internet Explorer and other older browser versions don't support the once option, so to support IE, you'll have to write a bit more code - manually remove the listener once it's triggered:






function listener() {
console.log('function running');
window.removeEventListener("keydown", listener);
}
window.addEventListener("keydown", listener);





Or, with the switch as well:






window.addEventListener("keydown", listener);

function listener(event) {
console.log('function running');
window.removeEventListener("keydown", listener);

switch (event.code) {
case 'Digit1':
shoot('Ball1');
break;

case 'Digit2':
shoot('Ball2');
break;
}
}





Again, with more switch cases:






window.addEventListener("keydown", listener); 

function listener(event) {
console.log('function running');
window.removeEventListener("keydown", listener);

switch(event.code){
case 'Digit1':
shoot('Ball1');
break;

case 'Digit2':
shoot('Ball2');
break;

case 'Digit3':
shoot('Ball3');
break;

case 'Digit4':
shoot('Ball4');
break;
}
}








share|improve this answer























  • I have tryed it without success. It doesn't work in chrome or iexplorer :-(
    – NewbieXXL
    Nov 22 at 7:35












  • See snippet edited into answer, works perfectly fine for me
    – CertainPerformance
    Nov 22 at 7:37










  • I believe you. But why doesn't it work for me?
    – NewbieXXL
    Nov 22 at 7:38










  • For non-up-to-date browsers like IE, you can't use the once option, you'll have to do it manually with removeEventListener instead. Looks like browsers implemented it around the end of 2016
    – CertainPerformance
    Nov 22 at 7:41












  • I have replaced {once: true} with 'listener' function and added the function in my code. chrome still show the the endless behaviour and the iexplorer doesn't react on keydown.
    – NewbieXXL
    Nov 22 at 7:47




















up vote
1
down vote













The boolean parameter you're adding doesn't specify that the event handler should only be executing once. It relates to event capture.



To have the handler execute only once, you need to pass an options object:



window.addEventListener("keydown", function(event) { /* ... */ }, {once: true});





share|improve this answer





















    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53425813%2fwhy-does-the-event-fire-more-than-once%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    3
    down vote













    The third parameter provided to addEventListener is either an options object, or useCapture, a boolean. Because you passed a boolean, the interpreter is considering you to have indicated that you want the function to trigger in the capturing phase, rather than the bubbling phase - which is not something you care about.



    Pass an options object with a once property instead:



    window.addEventListener(
    "keydown",
    function(event){
    switch(event.code){
    case 'Digit1':
    shoot('Ball1');
    break;

    case 'Digit2':
    shoot('Ball2');
    break;
    }

    },
    { once: true }
    );





    window.addEventListener(
    "keydown",
    function(event){
    console.log('function running');
    },
    { once: true }
    );





    Internet Explorer and other older browser versions don't support the once option, so to support IE, you'll have to write a bit more code - manually remove the listener once it's triggered:






    function listener() {
    console.log('function running');
    window.removeEventListener("keydown", listener);
    }
    window.addEventListener("keydown", listener);





    Or, with the switch as well:






    window.addEventListener("keydown", listener);

    function listener(event) {
    console.log('function running');
    window.removeEventListener("keydown", listener);

    switch (event.code) {
    case 'Digit1':
    shoot('Ball1');
    break;

    case 'Digit2':
    shoot('Ball2');
    break;
    }
    }





    Again, with more switch cases:






    window.addEventListener("keydown", listener); 

    function listener(event) {
    console.log('function running');
    window.removeEventListener("keydown", listener);

    switch(event.code){
    case 'Digit1':
    shoot('Ball1');
    break;

    case 'Digit2':
    shoot('Ball2');
    break;

    case 'Digit3':
    shoot('Ball3');
    break;

    case 'Digit4':
    shoot('Ball4');
    break;
    }
    }








    share|improve this answer























    • I have tryed it without success. It doesn't work in chrome or iexplorer :-(
      – NewbieXXL
      Nov 22 at 7:35












    • See snippet edited into answer, works perfectly fine for me
      – CertainPerformance
      Nov 22 at 7:37










    • I believe you. But why doesn't it work for me?
      – NewbieXXL
      Nov 22 at 7:38










    • For non-up-to-date browsers like IE, you can't use the once option, you'll have to do it manually with removeEventListener instead. Looks like browsers implemented it around the end of 2016
      – CertainPerformance
      Nov 22 at 7:41












    • I have replaced {once: true} with 'listener' function and added the function in my code. chrome still show the the endless behaviour and the iexplorer doesn't react on keydown.
      – NewbieXXL
      Nov 22 at 7:47

















    up vote
    3
    down vote













    The third parameter provided to addEventListener is either an options object, or useCapture, a boolean. Because you passed a boolean, the interpreter is considering you to have indicated that you want the function to trigger in the capturing phase, rather than the bubbling phase - which is not something you care about.



    Pass an options object with a once property instead:



    window.addEventListener(
    "keydown",
    function(event){
    switch(event.code){
    case 'Digit1':
    shoot('Ball1');
    break;

    case 'Digit2':
    shoot('Ball2');
    break;
    }

    },
    { once: true }
    );





    window.addEventListener(
    "keydown",
    function(event){
    console.log('function running');
    },
    { once: true }
    );





    Internet Explorer and other older browser versions don't support the once option, so to support IE, you'll have to write a bit more code - manually remove the listener once it's triggered:






    function listener() {
    console.log('function running');
    window.removeEventListener("keydown", listener);
    }
    window.addEventListener("keydown", listener);





    Or, with the switch as well:






    window.addEventListener("keydown", listener);

    function listener(event) {
    console.log('function running');
    window.removeEventListener("keydown", listener);

    switch (event.code) {
    case 'Digit1':
    shoot('Ball1');
    break;

    case 'Digit2':
    shoot('Ball2');
    break;
    }
    }





    Again, with more switch cases:






    window.addEventListener("keydown", listener); 

    function listener(event) {
    console.log('function running');
    window.removeEventListener("keydown", listener);

    switch(event.code){
    case 'Digit1':
    shoot('Ball1');
    break;

    case 'Digit2':
    shoot('Ball2');
    break;

    case 'Digit3':
    shoot('Ball3');
    break;

    case 'Digit4':
    shoot('Ball4');
    break;
    }
    }








    share|improve this answer























    • I have tryed it without success. It doesn't work in chrome or iexplorer :-(
      – NewbieXXL
      Nov 22 at 7:35












    • See snippet edited into answer, works perfectly fine for me
      – CertainPerformance
      Nov 22 at 7:37










    • I believe you. But why doesn't it work for me?
      – NewbieXXL
      Nov 22 at 7:38










    • For non-up-to-date browsers like IE, you can't use the once option, you'll have to do it manually with removeEventListener instead. Looks like browsers implemented it around the end of 2016
      – CertainPerformance
      Nov 22 at 7:41












    • I have replaced {once: true} with 'listener' function and added the function in my code. chrome still show the the endless behaviour and the iexplorer doesn't react on keydown.
      – NewbieXXL
      Nov 22 at 7:47















    up vote
    3
    down vote










    up vote
    3
    down vote









    The third parameter provided to addEventListener is either an options object, or useCapture, a boolean. Because you passed a boolean, the interpreter is considering you to have indicated that you want the function to trigger in the capturing phase, rather than the bubbling phase - which is not something you care about.



    Pass an options object with a once property instead:



    window.addEventListener(
    "keydown",
    function(event){
    switch(event.code){
    case 'Digit1':
    shoot('Ball1');
    break;

    case 'Digit2':
    shoot('Ball2');
    break;
    }

    },
    { once: true }
    );





    window.addEventListener(
    "keydown",
    function(event){
    console.log('function running');
    },
    { once: true }
    );





    Internet Explorer and other older browser versions don't support the once option, so to support IE, you'll have to write a bit more code - manually remove the listener once it's triggered:






    function listener() {
    console.log('function running');
    window.removeEventListener("keydown", listener);
    }
    window.addEventListener("keydown", listener);





    Or, with the switch as well:






    window.addEventListener("keydown", listener);

    function listener(event) {
    console.log('function running');
    window.removeEventListener("keydown", listener);

    switch (event.code) {
    case 'Digit1':
    shoot('Ball1');
    break;

    case 'Digit2':
    shoot('Ball2');
    break;
    }
    }





    Again, with more switch cases:






    window.addEventListener("keydown", listener); 

    function listener(event) {
    console.log('function running');
    window.removeEventListener("keydown", listener);

    switch(event.code){
    case 'Digit1':
    shoot('Ball1');
    break;

    case 'Digit2':
    shoot('Ball2');
    break;

    case 'Digit3':
    shoot('Ball3');
    break;

    case 'Digit4':
    shoot('Ball4');
    break;
    }
    }








    share|improve this answer














    The third parameter provided to addEventListener is either an options object, or useCapture, a boolean. Because you passed a boolean, the interpreter is considering you to have indicated that you want the function to trigger in the capturing phase, rather than the bubbling phase - which is not something you care about.



    Pass an options object with a once property instead:



    window.addEventListener(
    "keydown",
    function(event){
    switch(event.code){
    case 'Digit1':
    shoot('Ball1');
    break;

    case 'Digit2':
    shoot('Ball2');
    break;
    }

    },
    { once: true }
    );





    window.addEventListener(
    "keydown",
    function(event){
    console.log('function running');
    },
    { once: true }
    );





    Internet Explorer and other older browser versions don't support the once option, so to support IE, you'll have to write a bit more code - manually remove the listener once it's triggered:






    function listener() {
    console.log('function running');
    window.removeEventListener("keydown", listener);
    }
    window.addEventListener("keydown", listener);





    Or, with the switch as well:






    window.addEventListener("keydown", listener);

    function listener(event) {
    console.log('function running');
    window.removeEventListener("keydown", listener);

    switch (event.code) {
    case 'Digit1':
    shoot('Ball1');
    break;

    case 'Digit2':
    shoot('Ball2');
    break;
    }
    }





    Again, with more switch cases:






    window.addEventListener("keydown", listener); 

    function listener(event) {
    console.log('function running');
    window.removeEventListener("keydown", listener);

    switch(event.code){
    case 'Digit1':
    shoot('Ball1');
    break;

    case 'Digit2':
    shoot('Ball2');
    break;

    case 'Digit3':
    shoot('Ball3');
    break;

    case 'Digit4':
    shoot('Ball4');
    break;
    }
    }








    window.addEventListener(
    "keydown",
    function(event){
    console.log('function running');
    },
    { once: true }
    );





    window.addEventListener(
    "keydown",
    function(event){
    console.log('function running');
    },
    { once: true }
    );





    function listener() {
    console.log('function running');
    window.removeEventListener("keydown", listener);
    }
    window.addEventListener("keydown", listener);





    function listener() {
    console.log('function running');
    window.removeEventListener("keydown", listener);
    }
    window.addEventListener("keydown", listener);





    window.addEventListener("keydown", listener);

    function listener(event) {
    console.log('function running');
    window.removeEventListener("keydown", listener);

    switch (event.code) {
    case 'Digit1':
    shoot('Ball1');
    break;

    case 'Digit2':
    shoot('Ball2');
    break;
    }
    }





    window.addEventListener("keydown", listener);

    function listener(event) {
    console.log('function running');
    window.removeEventListener("keydown", listener);

    switch (event.code) {
    case 'Digit1':
    shoot('Ball1');
    break;

    case 'Digit2':
    shoot('Ball2');
    break;
    }
    }





    window.addEventListener("keydown", listener); 

    function listener(event) {
    console.log('function running');
    window.removeEventListener("keydown", listener);

    switch(event.code){
    case 'Digit1':
    shoot('Ball1');
    break;

    case 'Digit2':
    shoot('Ball2');
    break;

    case 'Digit3':
    shoot('Ball3');
    break;

    case 'Digit4':
    shoot('Ball4');
    break;
    }
    }





    window.addEventListener("keydown", listener); 

    function listener(event) {
    console.log('function running');
    window.removeEventListener("keydown", listener);

    switch(event.code){
    case 'Digit1':
    shoot('Ball1');
    break;

    case 'Digit2':
    shoot('Ball2');
    break;

    case 'Digit3':
    shoot('Ball3');
    break;

    case 'Digit4':
    shoot('Ball4');
    break;
    }
    }






    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Nov 22 at 8:34

























    answered Nov 22 at 7:33









    CertainPerformance

    73.1k143454




    73.1k143454












    • I have tryed it without success. It doesn't work in chrome or iexplorer :-(
      – NewbieXXL
      Nov 22 at 7:35












    • See snippet edited into answer, works perfectly fine for me
      – CertainPerformance
      Nov 22 at 7:37










    • I believe you. But why doesn't it work for me?
      – NewbieXXL
      Nov 22 at 7:38










    • For non-up-to-date browsers like IE, you can't use the once option, you'll have to do it manually with removeEventListener instead. Looks like browsers implemented it around the end of 2016
      – CertainPerformance
      Nov 22 at 7:41












    • I have replaced {once: true} with 'listener' function and added the function in my code. chrome still show the the endless behaviour and the iexplorer doesn't react on keydown.
      – NewbieXXL
      Nov 22 at 7:47




















    • I have tryed it without success. It doesn't work in chrome or iexplorer :-(
      – NewbieXXL
      Nov 22 at 7:35












    • See snippet edited into answer, works perfectly fine for me
      – CertainPerformance
      Nov 22 at 7:37










    • I believe you. But why doesn't it work for me?
      – NewbieXXL
      Nov 22 at 7:38










    • For non-up-to-date browsers like IE, you can't use the once option, you'll have to do it manually with removeEventListener instead. Looks like browsers implemented it around the end of 2016
      – CertainPerformance
      Nov 22 at 7:41












    • I have replaced {once: true} with 'listener' function and added the function in my code. chrome still show the the endless behaviour and the iexplorer doesn't react on keydown.
      – NewbieXXL
      Nov 22 at 7:47


















    I have tryed it without success. It doesn't work in chrome or iexplorer :-(
    – NewbieXXL
    Nov 22 at 7:35






    I have tryed it without success. It doesn't work in chrome or iexplorer :-(
    – NewbieXXL
    Nov 22 at 7:35














    See snippet edited into answer, works perfectly fine for me
    – CertainPerformance
    Nov 22 at 7:37




    See snippet edited into answer, works perfectly fine for me
    – CertainPerformance
    Nov 22 at 7:37












    I believe you. But why doesn't it work for me?
    – NewbieXXL
    Nov 22 at 7:38




    I believe you. But why doesn't it work for me?
    – NewbieXXL
    Nov 22 at 7:38












    For non-up-to-date browsers like IE, you can't use the once option, you'll have to do it manually with removeEventListener instead. Looks like browsers implemented it around the end of 2016
    – CertainPerformance
    Nov 22 at 7:41






    For non-up-to-date browsers like IE, you can't use the once option, you'll have to do it manually with removeEventListener instead. Looks like browsers implemented it around the end of 2016
    – CertainPerformance
    Nov 22 at 7:41














    I have replaced {once: true} with 'listener' function and added the function in my code. chrome still show the the endless behaviour and the iexplorer doesn't react on keydown.
    – NewbieXXL
    Nov 22 at 7:47






    I have replaced {once: true} with 'listener' function and added the function in my code. chrome still show the the endless behaviour and the iexplorer doesn't react on keydown.
    – NewbieXXL
    Nov 22 at 7:47














    up vote
    1
    down vote













    The boolean parameter you're adding doesn't specify that the event handler should only be executing once. It relates to event capture.



    To have the handler execute only once, you need to pass an options object:



    window.addEventListener("keydown", function(event) { /* ... */ }, {once: true});





    share|improve this answer

























      up vote
      1
      down vote













      The boolean parameter you're adding doesn't specify that the event handler should only be executing once. It relates to event capture.



      To have the handler execute only once, you need to pass an options object:



      window.addEventListener("keydown", function(event) { /* ... */ }, {once: true});





      share|improve this answer























        up vote
        1
        down vote










        up vote
        1
        down vote









        The boolean parameter you're adding doesn't specify that the event handler should only be executing once. It relates to event capture.



        To have the handler execute only once, you need to pass an options object:



        window.addEventListener("keydown", function(event) { /* ... */ }, {once: true});





        share|improve this answer












        The boolean parameter you're adding doesn't specify that the event handler should only be executing once. It relates to event capture.



        To have the handler execute only once, you need to pass an options object:



        window.addEventListener("keydown", function(event) { /* ... */ }, {once: true});






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 22 at 7:33









        Robby Cornelissen

        42.9k126789




        42.9k126789






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53425813%2fwhy-does-the-event-fire-more-than-once%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Berounka

            Fiat S.p.A.

            Type 'String' is not a subtype of type 'int' of 'index'