How to code a button that only works when clicked multiple times Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 23, 2019 at 23:30UTC (7:30pm US/Eastern) Data science time! April 2019 and salary with experience The Ask Question Wizard is Live!How do JavaScript closures work?How do I detect a click outside an element?Trigger a button click with JavaScript on the Enter key in a text boxHow does JavaScript .prototype work?How can I know which radio button is selected via jQuery?How can I select an element with multiple classes in jQuery?How does the “this” keyword work?How to decide when to use Node.js?How does data binding work in AngularJS?How to find what code is run by a button/element in Chrome using Developer Tools

What would you call this weird metallic apparatus that allows you to lift people?

How to pronounce 伝統色

How did Fremen produce and carry enough thumpers to use Sandworms as de facto Ubers?

A letter with no particular backstory

Is multiple magic items in one inherently imbalanced?

Project Euler #1 in C++

What is the difference between a "ranged attack" and a "ranged weapon attack"?

Deconstruction is ambiguous

Why does 14 CFR have skipped subparts in my ASA 2019 FAR/AIM book?

An adverb for when you're not exaggerating

Misunderstanding of Sylow theory

C's equality operator on converted pointers

Do I really need to have a message in a novel to appeal to readers?

Has negative voting ever been officially implemented in elections, or seriously proposed, or even studied?

Drawing spherical mirrors

Central Vacuuming: Is it worth it, and how does it compare to normal vacuuming?

What initially awakened the Balrog?

How can I prevent/balance waiting and turtling as a response to cooldown mechanics

What does Turing mean by this statement?

One-one communication

How many time has Arya actually used Needle?

How to write capital alpha?

How often does castling occur in grandmaster games?

How to compare two different files line by line in unix?



How to code a button that only works when clicked multiple times



Announcing the arrival of Valued Associate #679: Cesar Manara
Planned maintenance scheduled April 23, 2019 at 23:30UTC (7:30pm US/Eastern)
Data science time! April 2019 and salary with experience
The Ask Question Wizard is Live!How do JavaScript closures work?How do I detect a click outside an element?Trigger a button click with JavaScript on the Enter key in a text boxHow does JavaScript .prototype work?How can I know which radio button is selected via jQuery?How can I select an element with multiple classes in jQuery?How does the “this” keyword work?How to decide when to use Node.js?How does data binding work in AngularJS?How to find what code is run by a button/element in Chrome using Developer Tools



.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








1















I am trying to code a simple quiz app. I am trying to put a hidden screen at the end when one clicks on a button 3 times at the end. This is what I have tried:



for (var i = 0; i > 2; i++) 
onEvent("button26", "click", function()
setScreen("TrollScreen");
playSound("sound://default.mp3", false);
);



I am fairly new to code, and I'm not sure how to do this. Help is appreciated.










share|improve this question



















  • 1





    for (var i = 0; i > 2; i++) { will never execute code inside of it

    – j08691
    Mar 8 at 21:58











  • @j08691 He's new to coding, please explain him that he made a typo, and that i>2 isn't resolving as true, therefore it wont execute any code, otherwise you might be introducing yourself the belief that a "for loop doesn't execute code" which isn't your goal

    – Alejandro Vales
    Mar 8 at 22:08


















1















I am trying to code a simple quiz app. I am trying to put a hidden screen at the end when one clicks on a button 3 times at the end. This is what I have tried:



for (var i = 0; i > 2; i++) 
onEvent("button26", "click", function()
setScreen("TrollScreen");
playSound("sound://default.mp3", false);
);



I am fairly new to code, and I'm not sure how to do this. Help is appreciated.










share|improve this question



















  • 1





    for (var i = 0; i > 2; i++) { will never execute code inside of it

    – j08691
    Mar 8 at 21:58











  • @j08691 He's new to coding, please explain him that he made a typo, and that i>2 isn't resolving as true, therefore it wont execute any code, otherwise you might be introducing yourself the belief that a "for loop doesn't execute code" which isn't your goal

    – Alejandro Vales
    Mar 8 at 22:08














1












1








1








I am trying to code a simple quiz app. I am trying to put a hidden screen at the end when one clicks on a button 3 times at the end. This is what I have tried:



for (var i = 0; i > 2; i++) 
onEvent("button26", "click", function()
setScreen("TrollScreen");
playSound("sound://default.mp3", false);
);



I am fairly new to code, and I'm not sure how to do this. Help is appreciated.










share|improve this question
















I am trying to code a simple quiz app. I am trying to put a hidden screen at the end when one clicks on a button 3 times at the end. This is what I have tried:



for (var i = 0; i > 2; i++) 
onEvent("button26", "click", function()
setScreen("TrollScreen");
playSound("sound://default.mp3", false);
);



I am fairly new to code, and I'm not sure how to do this. Help is appreciated.







javascript






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 8 at 21:51









ibrahim mahrir

22.4k41952




22.4k41952










asked Mar 8 at 21:50









Max0815Max0815

1106




1106







  • 1





    for (var i = 0; i > 2; i++) { will never execute code inside of it

    – j08691
    Mar 8 at 21:58











  • @j08691 He's new to coding, please explain him that he made a typo, and that i>2 isn't resolving as true, therefore it wont execute any code, otherwise you might be introducing yourself the belief that a "for loop doesn't execute code" which isn't your goal

    – Alejandro Vales
    Mar 8 at 22:08













  • 1





    for (var i = 0; i > 2; i++) { will never execute code inside of it

    – j08691
    Mar 8 at 21:58











  • @j08691 He's new to coding, please explain him that he made a typo, and that i>2 isn't resolving as true, therefore it wont execute any code, otherwise you might be introducing yourself the belief that a "for loop doesn't execute code" which isn't your goal

    – Alejandro Vales
    Mar 8 at 22:08








1




1





for (var i = 0; i > 2; i++) { will never execute code inside of it

– j08691
Mar 8 at 21:58





for (var i = 0; i > 2; i++) { will never execute code inside of it

– j08691
Mar 8 at 21:58













@j08691 He's new to coding, please explain him that he made a typo, and that i>2 isn't resolving as true, therefore it wont execute any code, otherwise you might be introducing yourself the belief that a "for loop doesn't execute code" which isn't your goal

– Alejandro Vales
Mar 8 at 22:08






@j08691 He's new to coding, please explain him that he made a typo, and that i>2 isn't resolving as true, therefore it wont execute any code, otherwise you might be introducing yourself the belief that a "for loop doesn't execute code" which isn't your goal

– Alejandro Vales
Mar 8 at 22:08













6 Answers
6






active

oldest

votes


















3














You need to keep the count of the clicks outside of the event handler. Then inside it you can check that value and show the screen or increase the counter accordingly.



var count = 0;
onEvent("button26", "click", function()
if(count > 2)
setScreen("TrollScreen");
playSound("sound://default.mp3", false);
else
count++;

);





share|improve this answer






























    2














    Since all DOM elements are actually objects, you can attach a property to them that will serve as a counter, thus when a button gets clicked, you increment that property by 1 and then check if it reached 3 already.



    A more subtle approach is to use a helper function that attaches the event and set up the counter as a closured variable, here is how:



    function attachEventWithCounter(elem, func, maxClickCount) 
    let count = 0;
    elem.addEventListener("click", function(e)
    count++;
    if(count >= maxClickCount)
    func.call(this, e);
    // and probably reset 'count' to 0

    );



    You can then use it like so:



    attachEventWithCounter(myButton, myEventListener, 3);


    attachEventWithCounter just takes a DOM element, a function that will serve as the event listener and a number that will be the maximum amount of tries. It then attaches a click event listener (you could pass in the type of the event as well if you want) and then whenever that event happens, it increments a locally declared variable count (initially set to 0) and checks if it reached the maximum amount of tries, if so it just calls the function passed as parameter (using Function#call to pass a custom this and the event argument to mimic the actual event listener).



    Example:






    function attachEventWithCounter(elem, func, maxClickCount) 
    let count = 0;
    elem.addEventListener("click", function(e)
    count++;
    if(count >= maxClickCount)
    func.call(this, e);
    count = 0;

    );



    let btn = document.getElementById("myButton");

    function listener()
    alert("Clicked at last!!!");


    attachEventWithCounter(btn, listener, 3);

    <button id="myButton">Click me 3 times</button>








    share|improve this answer
































      0














      this will click the button three times every time you press it (at least I think). instead, make a counter variable that starts at 0 and increment it up by 1 each time the button is pressed. the put the action you want to perform inside in an if statement ie



      if(counter >= 3)
      //do some action



      hope that helps!






      share|improve this answer






























        0














        you want to keep a counter variable outside the scope of the event to keep track of how many times it was clicked. Ex.



        let counter = 0;
        onEvent("button26", "click", function()
        if(counter >= 3)
        setScreen("TrollScreen");
        playSound("sound://default.mp3", false);

        counter ++;
        );





        share|improve this answer






























          0

















           //create a variable to check how many times the button has been clicked
          var buttonClick = 0;

          function CheckCount()
          //Check if the buttonClick variable is three
          if (buttonClick == 3)
          //if it is equal to three, display the screen and play the sound

          //below commented out for sake of demo
          //setScreen("TrollScreen");
          //playSound("sound://default.mp3", false);
          document.getElementById('buttonclickcount').innerHTML = "You clicked it three times";

          else
          //if it is not so, then increment the buttonClick variable by 1
          buttonClick++

          //so you can see how many times the button has been clicked
          document.getElementById('buttonclickcount').innerHTML = buttonClick;

          ;

          <!--I would create an onclick event on the button itself that runs a function when -->
          <button onclick="CheckCount()">You Win!</button>

          <div id="buttonclickcount"></div>








          share|improve this answer






























            0














            You should look at using closures. This is where you define a variable before returning a function; your returned function closes around this variable. You could do something like in this fiddle.



            const button = document.getElementById('button');
            const output = document.getElementById('output');

            const click = (function()
            let count = 0;
            return function()
            count++;
            if(count > 3)
            output.innerHTML = 'Count is greater than 3: ' + count


            )();

            button.addEventListener('click', click);





            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%2f55071490%2fhow-to-code-a-button-that-only-works-when-clicked-multiple-times%23new-answer', 'question_page');

              );

              Post as a guest















              Required, but never shown

























              6 Answers
              6






              active

              oldest

              votes








              6 Answers
              6






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes









              3














              You need to keep the count of the clicks outside of the event handler. Then inside it you can check that value and show the screen or increase the counter accordingly.



              var count = 0;
              onEvent("button26", "click", function()
              if(count > 2)
              setScreen("TrollScreen");
              playSound("sound://default.mp3", false);
              else
              count++;

              );





              share|improve this answer



























                3














                You need to keep the count of the clicks outside of the event handler. Then inside it you can check that value and show the screen or increase the counter accordingly.



                var count = 0;
                onEvent("button26", "click", function()
                if(count > 2)
                setScreen("TrollScreen");
                playSound("sound://default.mp3", false);
                else
                count++;

                );





                share|improve this answer

























                  3












                  3








                  3







                  You need to keep the count of the clicks outside of the event handler. Then inside it you can check that value and show the screen or increase the counter accordingly.



                  var count = 0;
                  onEvent("button26", "click", function()
                  if(count > 2)
                  setScreen("TrollScreen");
                  playSound("sound://default.mp3", false);
                  else
                  count++;

                  );





                  share|improve this answer













                  You need to keep the count of the clicks outside of the event handler. Then inside it you can check that value and show the screen or increase the counter accordingly.



                  var count = 0;
                  onEvent("button26", "click", function()
                  if(count > 2)
                  setScreen("TrollScreen");
                  playSound("sound://default.mp3", false);
                  else
                  count++;

                  );






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Mar 8 at 21:57









                  Velimir TchatchevskyVelimir Tchatchevsky

                  2,26011118




                  2,26011118























                      2














                      Since all DOM elements are actually objects, you can attach a property to them that will serve as a counter, thus when a button gets clicked, you increment that property by 1 and then check if it reached 3 already.



                      A more subtle approach is to use a helper function that attaches the event and set up the counter as a closured variable, here is how:



                      function attachEventWithCounter(elem, func, maxClickCount) 
                      let count = 0;
                      elem.addEventListener("click", function(e)
                      count++;
                      if(count >= maxClickCount)
                      func.call(this, e);
                      // and probably reset 'count' to 0

                      );



                      You can then use it like so:



                      attachEventWithCounter(myButton, myEventListener, 3);


                      attachEventWithCounter just takes a DOM element, a function that will serve as the event listener and a number that will be the maximum amount of tries. It then attaches a click event listener (you could pass in the type of the event as well if you want) and then whenever that event happens, it increments a locally declared variable count (initially set to 0) and checks if it reached the maximum amount of tries, if so it just calls the function passed as parameter (using Function#call to pass a custom this and the event argument to mimic the actual event listener).



                      Example:






                      function attachEventWithCounter(elem, func, maxClickCount) 
                      let count = 0;
                      elem.addEventListener("click", function(e)
                      count++;
                      if(count >= maxClickCount)
                      func.call(this, e);
                      count = 0;

                      );



                      let btn = document.getElementById("myButton");

                      function listener()
                      alert("Clicked at last!!!");


                      attachEventWithCounter(btn, listener, 3);

                      <button id="myButton">Click me 3 times</button>








                      share|improve this answer





























                        2














                        Since all DOM elements are actually objects, you can attach a property to them that will serve as a counter, thus when a button gets clicked, you increment that property by 1 and then check if it reached 3 already.



                        A more subtle approach is to use a helper function that attaches the event and set up the counter as a closured variable, here is how:



                        function attachEventWithCounter(elem, func, maxClickCount) 
                        let count = 0;
                        elem.addEventListener("click", function(e)
                        count++;
                        if(count >= maxClickCount)
                        func.call(this, e);
                        // and probably reset 'count' to 0

                        );



                        You can then use it like so:



                        attachEventWithCounter(myButton, myEventListener, 3);


                        attachEventWithCounter just takes a DOM element, a function that will serve as the event listener and a number that will be the maximum amount of tries. It then attaches a click event listener (you could pass in the type of the event as well if you want) and then whenever that event happens, it increments a locally declared variable count (initially set to 0) and checks if it reached the maximum amount of tries, if so it just calls the function passed as parameter (using Function#call to pass a custom this and the event argument to mimic the actual event listener).



                        Example:






                        function attachEventWithCounter(elem, func, maxClickCount) 
                        let count = 0;
                        elem.addEventListener("click", function(e)
                        count++;
                        if(count >= maxClickCount)
                        func.call(this, e);
                        count = 0;

                        );



                        let btn = document.getElementById("myButton");

                        function listener()
                        alert("Clicked at last!!!");


                        attachEventWithCounter(btn, listener, 3);

                        <button id="myButton">Click me 3 times</button>








                        share|improve this answer



























                          2












                          2








                          2







                          Since all DOM elements are actually objects, you can attach a property to them that will serve as a counter, thus when a button gets clicked, you increment that property by 1 and then check if it reached 3 already.



                          A more subtle approach is to use a helper function that attaches the event and set up the counter as a closured variable, here is how:



                          function attachEventWithCounter(elem, func, maxClickCount) 
                          let count = 0;
                          elem.addEventListener("click", function(e)
                          count++;
                          if(count >= maxClickCount)
                          func.call(this, e);
                          // and probably reset 'count' to 0

                          );



                          You can then use it like so:



                          attachEventWithCounter(myButton, myEventListener, 3);


                          attachEventWithCounter just takes a DOM element, a function that will serve as the event listener and a number that will be the maximum amount of tries. It then attaches a click event listener (you could pass in the type of the event as well if you want) and then whenever that event happens, it increments a locally declared variable count (initially set to 0) and checks if it reached the maximum amount of tries, if so it just calls the function passed as parameter (using Function#call to pass a custom this and the event argument to mimic the actual event listener).



                          Example:






                          function attachEventWithCounter(elem, func, maxClickCount) 
                          let count = 0;
                          elem.addEventListener("click", function(e)
                          count++;
                          if(count >= maxClickCount)
                          func.call(this, e);
                          count = 0;

                          );



                          let btn = document.getElementById("myButton");

                          function listener()
                          alert("Clicked at last!!!");


                          attachEventWithCounter(btn, listener, 3);

                          <button id="myButton">Click me 3 times</button>








                          share|improve this answer















                          Since all DOM elements are actually objects, you can attach a property to them that will serve as a counter, thus when a button gets clicked, you increment that property by 1 and then check if it reached 3 already.



                          A more subtle approach is to use a helper function that attaches the event and set up the counter as a closured variable, here is how:



                          function attachEventWithCounter(elem, func, maxClickCount) 
                          let count = 0;
                          elem.addEventListener("click", function(e)
                          count++;
                          if(count >= maxClickCount)
                          func.call(this, e);
                          // and probably reset 'count' to 0

                          );



                          You can then use it like so:



                          attachEventWithCounter(myButton, myEventListener, 3);


                          attachEventWithCounter just takes a DOM element, a function that will serve as the event listener and a number that will be the maximum amount of tries. It then attaches a click event listener (you could pass in the type of the event as well if you want) and then whenever that event happens, it increments a locally declared variable count (initially set to 0) and checks if it reached the maximum amount of tries, if so it just calls the function passed as parameter (using Function#call to pass a custom this and the event argument to mimic the actual event listener).



                          Example:






                          function attachEventWithCounter(elem, func, maxClickCount) 
                          let count = 0;
                          elem.addEventListener("click", function(e)
                          count++;
                          if(count >= maxClickCount)
                          func.call(this, e);
                          count = 0;

                          );



                          let btn = document.getElementById("myButton");

                          function listener()
                          alert("Clicked at last!!!");


                          attachEventWithCounter(btn, listener, 3);

                          <button id="myButton">Click me 3 times</button>








                          function attachEventWithCounter(elem, func, maxClickCount) 
                          let count = 0;
                          elem.addEventListener("click", function(e)
                          count++;
                          if(count >= maxClickCount)
                          func.call(this, e);
                          count = 0;

                          );



                          let btn = document.getElementById("myButton");

                          function listener()
                          alert("Clicked at last!!!");


                          attachEventWithCounter(btn, listener, 3);

                          <button id="myButton">Click me 3 times</button>





                          function attachEventWithCounter(elem, func, maxClickCount) 
                          let count = 0;
                          elem.addEventListener("click", function(e)
                          count++;
                          if(count >= maxClickCount)
                          func.call(this, e);
                          count = 0;

                          );



                          let btn = document.getElementById("myButton");

                          function listener()
                          alert("Clicked at last!!!");


                          attachEventWithCounter(btn, listener, 3);

                          <button id="myButton">Click me 3 times</button>






                          share|improve this answer














                          share|improve this answer



                          share|improve this answer








                          edited Mar 8 at 22:07

























                          answered Mar 8 at 21:57









                          ibrahim mahriribrahim mahrir

                          22.4k41952




                          22.4k41952





















                              0














                              this will click the button three times every time you press it (at least I think). instead, make a counter variable that starts at 0 and increment it up by 1 each time the button is pressed. the put the action you want to perform inside in an if statement ie



                              if(counter >= 3)
                              //do some action



                              hope that helps!






                              share|improve this answer



























                                0














                                this will click the button three times every time you press it (at least I think). instead, make a counter variable that starts at 0 and increment it up by 1 each time the button is pressed. the put the action you want to perform inside in an if statement ie



                                if(counter >= 3)
                                //do some action



                                hope that helps!






                                share|improve this answer

























                                  0












                                  0








                                  0







                                  this will click the button three times every time you press it (at least I think). instead, make a counter variable that starts at 0 and increment it up by 1 each time the button is pressed. the put the action you want to perform inside in an if statement ie



                                  if(counter >= 3)
                                  //do some action



                                  hope that helps!






                                  share|improve this answer













                                  this will click the button three times every time you press it (at least I think). instead, make a counter variable that starts at 0 and increment it up by 1 each time the button is pressed. the put the action you want to perform inside in an if statement ie



                                  if(counter >= 3)
                                  //do some action



                                  hope that helps!







                                  share|improve this answer












                                  share|improve this answer



                                  share|improve this answer










                                  answered Mar 8 at 21:56









                                  jason.dachmanjason.dachman

                                  341




                                  341





















                                      0














                                      you want to keep a counter variable outside the scope of the event to keep track of how many times it was clicked. Ex.



                                      let counter = 0;
                                      onEvent("button26", "click", function()
                                      if(counter >= 3)
                                      setScreen("TrollScreen");
                                      playSound("sound://default.mp3", false);

                                      counter ++;
                                      );





                                      share|improve this answer



























                                        0














                                        you want to keep a counter variable outside the scope of the event to keep track of how many times it was clicked. Ex.



                                        let counter = 0;
                                        onEvent("button26", "click", function()
                                        if(counter >= 3)
                                        setScreen("TrollScreen");
                                        playSound("sound://default.mp3", false);

                                        counter ++;
                                        );





                                        share|improve this answer

























                                          0












                                          0








                                          0







                                          you want to keep a counter variable outside the scope of the event to keep track of how many times it was clicked. Ex.



                                          let counter = 0;
                                          onEvent("button26", "click", function()
                                          if(counter >= 3)
                                          setScreen("TrollScreen");
                                          playSound("sound://default.mp3", false);

                                          counter ++;
                                          );





                                          share|improve this answer













                                          you want to keep a counter variable outside the scope of the event to keep track of how many times it was clicked. Ex.



                                          let counter = 0;
                                          onEvent("button26", "click", function()
                                          if(counter >= 3)
                                          setScreen("TrollScreen");
                                          playSound("sound://default.mp3", false);

                                          counter ++;
                                          );






                                          share|improve this answer












                                          share|improve this answer



                                          share|improve this answer










                                          answered Mar 8 at 21:58









                                          Marc Sloth EastmanMarc Sloth Eastman

                                          15810




                                          15810





















                                              0

















                                               //create a variable to check how many times the button has been clicked
                                              var buttonClick = 0;

                                              function CheckCount()
                                              //Check if the buttonClick variable is three
                                              if (buttonClick == 3)
                                              //if it is equal to three, display the screen and play the sound

                                              //below commented out for sake of demo
                                              //setScreen("TrollScreen");
                                              //playSound("sound://default.mp3", false);
                                              document.getElementById('buttonclickcount').innerHTML = "You clicked it three times";

                                              else
                                              //if it is not so, then increment the buttonClick variable by 1
                                              buttonClick++

                                              //so you can see how many times the button has been clicked
                                              document.getElementById('buttonclickcount').innerHTML = buttonClick;

                                              ;

                                              <!--I would create an onclick event on the button itself that runs a function when -->
                                              <button onclick="CheckCount()">You Win!</button>

                                              <div id="buttonclickcount"></div>








                                              share|improve this answer



























                                                0

















                                                 //create a variable to check how many times the button has been clicked
                                                var buttonClick = 0;

                                                function CheckCount()
                                                //Check if the buttonClick variable is three
                                                if (buttonClick == 3)
                                                //if it is equal to three, display the screen and play the sound

                                                //below commented out for sake of demo
                                                //setScreen("TrollScreen");
                                                //playSound("sound://default.mp3", false);
                                                document.getElementById('buttonclickcount').innerHTML = "You clicked it three times";

                                                else
                                                //if it is not so, then increment the buttonClick variable by 1
                                                buttonClick++

                                                //so you can see how many times the button has been clicked
                                                document.getElementById('buttonclickcount').innerHTML = buttonClick;

                                                ;

                                                <!--I would create an onclick event on the button itself that runs a function when -->
                                                <button onclick="CheckCount()">You Win!</button>

                                                <div id="buttonclickcount"></div>








                                                share|improve this answer

























                                                  0












                                                  0








                                                  0










                                                   //create a variable to check how many times the button has been clicked
                                                  var buttonClick = 0;

                                                  function CheckCount()
                                                  //Check if the buttonClick variable is three
                                                  if (buttonClick == 3)
                                                  //if it is equal to three, display the screen and play the sound

                                                  //below commented out for sake of demo
                                                  //setScreen("TrollScreen");
                                                  //playSound("sound://default.mp3", false);
                                                  document.getElementById('buttonclickcount').innerHTML = "You clicked it three times";

                                                  else
                                                  //if it is not so, then increment the buttonClick variable by 1
                                                  buttonClick++

                                                  //so you can see how many times the button has been clicked
                                                  document.getElementById('buttonclickcount').innerHTML = buttonClick;

                                                  ;

                                                  <!--I would create an onclick event on the button itself that runs a function when -->
                                                  <button onclick="CheckCount()">You Win!</button>

                                                  <div id="buttonclickcount"></div>








                                                  share|improve this answer
















                                                   //create a variable to check how many times the button has been clicked
                                                  var buttonClick = 0;

                                                  function CheckCount()
                                                  //Check if the buttonClick variable is three
                                                  if (buttonClick == 3)
                                                  //if it is equal to three, display the screen and play the sound

                                                  //below commented out for sake of demo
                                                  //setScreen("TrollScreen");
                                                  //playSound("sound://default.mp3", false);
                                                  document.getElementById('buttonclickcount').innerHTML = "You clicked it three times";

                                                  else
                                                  //if it is not so, then increment the buttonClick variable by 1
                                                  buttonClick++

                                                  //so you can see how many times the button has been clicked
                                                  document.getElementById('buttonclickcount').innerHTML = buttonClick;

                                                  ;

                                                  <!--I would create an onclick event on the button itself that runs a function when -->
                                                  <button onclick="CheckCount()">You Win!</button>

                                                  <div id="buttonclickcount"></div>








                                                   //create a variable to check how many times the button has been clicked
                                                  var buttonClick = 0;

                                                  function CheckCount()
                                                  //Check if the buttonClick variable is three
                                                  if (buttonClick == 3)
                                                  //if it is equal to three, display the screen and play the sound

                                                  //below commented out for sake of demo
                                                  //setScreen("TrollScreen");
                                                  //playSound("sound://default.mp3", false);
                                                  document.getElementById('buttonclickcount').innerHTML = "You clicked it three times";

                                                  else
                                                  //if it is not so, then increment the buttonClick variable by 1
                                                  buttonClick++

                                                  //so you can see how many times the button has been clicked
                                                  document.getElementById('buttonclickcount').innerHTML = buttonClick;

                                                  ;

                                                  <!--I would create an onclick event on the button itself that runs a function when -->
                                                  <button onclick="CheckCount()">You Win!</button>

                                                  <div id="buttonclickcount"></div>





                                                   //create a variable to check how many times the button has been clicked
                                                  var buttonClick = 0;

                                                  function CheckCount()
                                                  //Check if the buttonClick variable is three
                                                  if (buttonClick == 3)
                                                  //if it is equal to three, display the screen and play the sound

                                                  //below commented out for sake of demo
                                                  //setScreen("TrollScreen");
                                                  //playSound("sound://default.mp3", false);
                                                  document.getElementById('buttonclickcount').innerHTML = "You clicked it three times";

                                                  else
                                                  //if it is not so, then increment the buttonClick variable by 1
                                                  buttonClick++

                                                  //so you can see how many times the button has been clicked
                                                  document.getElementById('buttonclickcount').innerHTML = buttonClick;

                                                  ;

                                                  <!--I would create an onclick event on the button itself that runs a function when -->
                                                  <button onclick="CheckCount()">You Win!</button>

                                                  <div id="buttonclickcount"></div>






                                                  share|improve this answer












                                                  share|improve this answer



                                                  share|improve this answer










                                                  answered Mar 8 at 22:01









                                                  JhWebDevGuyJhWebDevGuy

                                                  7639




                                                  7639





















                                                      0














                                                      You should look at using closures. This is where you define a variable before returning a function; your returned function closes around this variable. You could do something like in this fiddle.



                                                      const button = document.getElementById('button');
                                                      const output = document.getElementById('output');

                                                      const click = (function()
                                                      let count = 0;
                                                      return function()
                                                      count++;
                                                      if(count > 3)
                                                      output.innerHTML = 'Count is greater than 3: ' + count


                                                      )();

                                                      button.addEventListener('click', click);





                                                      share|improve this answer



























                                                        0














                                                        You should look at using closures. This is where you define a variable before returning a function; your returned function closes around this variable. You could do something like in this fiddle.



                                                        const button = document.getElementById('button');
                                                        const output = document.getElementById('output');

                                                        const click = (function()
                                                        let count = 0;
                                                        return function()
                                                        count++;
                                                        if(count > 3)
                                                        output.innerHTML = 'Count is greater than 3: ' + count


                                                        )();

                                                        button.addEventListener('click', click);





                                                        share|improve this answer

























                                                          0












                                                          0








                                                          0







                                                          You should look at using closures. This is where you define a variable before returning a function; your returned function closes around this variable. You could do something like in this fiddle.



                                                          const button = document.getElementById('button');
                                                          const output = document.getElementById('output');

                                                          const click = (function()
                                                          let count = 0;
                                                          return function()
                                                          count++;
                                                          if(count > 3)
                                                          output.innerHTML = 'Count is greater than 3: ' + count


                                                          )();

                                                          button.addEventListener('click', click);





                                                          share|improve this answer













                                                          You should look at using closures. This is where you define a variable before returning a function; your returned function closes around this variable. You could do something like in this fiddle.



                                                          const button = document.getElementById('button');
                                                          const output = document.getElementById('output');

                                                          const click = (function()
                                                          let count = 0;
                                                          return function()
                                                          count++;
                                                          if(count > 3)
                                                          output.innerHTML = 'Count is greater than 3: ' + count


                                                          )();

                                                          button.addEventListener('click', click);






                                                          share|improve this answer












                                                          share|improve this answer



                                                          share|improve this answer










                                                          answered Mar 8 at 22:08









                                                          MattMatt

                                                          1807




                                                          1807



























                                                              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.




                                                              draft saved


                                                              draft discarded














                                                              StackExchange.ready(
                                                              function ()
                                                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55071490%2fhow-to-code-a-button-that-only-works-when-clicked-multiple-times%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

                                                              AWS Lex not identifying response if by a variable The 2019 Stack Overflow Developer Survey Results Are In Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern) The Ask Question Wizard is Live! Data science time! April 2019 and salary with experienceEnforcing custom enumeration in AWS LEX for slot valuesHow to give response based on user response in Amazon Lex?Intercepting AWS Lambda Response to a AWS Lex QueryLex chat bot error: Reached second execution of fulfillment lambda on the same utteranceamazon lex showing invalid responseLambda response send back to Lex slot?Response card in Amazon lexAmazon Lex - Lambda response return HTML to botHow can I solve 424 (Failed Dependency) (python) obtained from Amazon lex?

                                                              Алба-Юлія

                                                              Захаров Федір Захарович