User clicks too fast bug





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







-1















first of all i'm pretty new to JS.
i'm trying to make a memory game but i have the next problem.
the code :



function cardClicked(elCard) {
// If the user clicked an already flipped card - do nothing and return from the function
if (elCard.classList.contains('flipped')) {
return;
}
// Flip it
elCard.classList.add('flipped');

// This is a first card, only keep it in the global variable
if (elPreviousCard === null) {
elPreviousCard = elCard;

} else {
// get the data-card attribute's value from both cards
var card1 = elPreviousCard.getAttribute('data-card');
var card2 = elCard.getAttribute('data-card');
// No match, schedule to flip them back in 1 second
if (card1 !== card2) {

setTimeout(function () {
elCard.classList.remove('flipped');
elPreviousCard.classList.remove('flipped');
elPreviousCard = null;
}, 1000)
wrongPick.play();
} else {
// Yes! a match!
flippedCouplesCount++;
elPreviousCard = null;
rightPick.play();
}
}




i would like to prevent the user from clicking more then two times (it's a memory game.) can someone tell me how can i implement this into my code please?










share|improve this question

























  • Have you tried preventing the user from clicking twice? What did you try?

    – iagowp
    Nov 25 '18 at 0:24













  • By saying preventing the user from clicking twice i mean clicking on more then 2 cards. (the game goes like that -> you pick a card -> then you pick another card. -> if its a match the cards stays. if not cards fade after 10sec.

    – Free Gems
    Nov 25 '18 at 0:27











  • Can you explain a little better? What scenario are you trying to prevent? Is it when a user has already picked one card, then he immediately picks the second, and a third, or just picking two cards in a small timeframe? What interval should there be between clicks?

    – iagowp
    Nov 25 '18 at 0:30











  • The senario i'm trying to prevent is fast clicking. if the user clicks on 3 cards (quick). he can bug the code. Or prevent the code from giving a delay (the 10 sec delay). But what im trying to achive is after the user clicking 2 times maybe lock all the other cards. until the cards go back to default..

    – Free Gems
    Nov 25 '18 at 0:33




















-1















first of all i'm pretty new to JS.
i'm trying to make a memory game but i have the next problem.
the code :



function cardClicked(elCard) {
// If the user clicked an already flipped card - do nothing and return from the function
if (elCard.classList.contains('flipped')) {
return;
}
// Flip it
elCard.classList.add('flipped');

// This is a first card, only keep it in the global variable
if (elPreviousCard === null) {
elPreviousCard = elCard;

} else {
// get the data-card attribute's value from both cards
var card1 = elPreviousCard.getAttribute('data-card');
var card2 = elCard.getAttribute('data-card');
// No match, schedule to flip them back in 1 second
if (card1 !== card2) {

setTimeout(function () {
elCard.classList.remove('flipped');
elPreviousCard.classList.remove('flipped');
elPreviousCard = null;
}, 1000)
wrongPick.play();
} else {
// Yes! a match!
flippedCouplesCount++;
elPreviousCard = null;
rightPick.play();
}
}




i would like to prevent the user from clicking more then two times (it's a memory game.) can someone tell me how can i implement this into my code please?










share|improve this question

























  • Have you tried preventing the user from clicking twice? What did you try?

    – iagowp
    Nov 25 '18 at 0:24













  • By saying preventing the user from clicking twice i mean clicking on more then 2 cards. (the game goes like that -> you pick a card -> then you pick another card. -> if its a match the cards stays. if not cards fade after 10sec.

    – Free Gems
    Nov 25 '18 at 0:27











  • Can you explain a little better? What scenario are you trying to prevent? Is it when a user has already picked one card, then he immediately picks the second, and a third, or just picking two cards in a small timeframe? What interval should there be between clicks?

    – iagowp
    Nov 25 '18 at 0:30











  • The senario i'm trying to prevent is fast clicking. if the user clicks on 3 cards (quick). he can bug the code. Or prevent the code from giving a delay (the 10 sec delay). But what im trying to achive is after the user clicking 2 times maybe lock all the other cards. until the cards go back to default..

    – Free Gems
    Nov 25 '18 at 0:33
















-1












-1








-1








first of all i'm pretty new to JS.
i'm trying to make a memory game but i have the next problem.
the code :



function cardClicked(elCard) {
// If the user clicked an already flipped card - do nothing and return from the function
if (elCard.classList.contains('flipped')) {
return;
}
// Flip it
elCard.classList.add('flipped');

// This is a first card, only keep it in the global variable
if (elPreviousCard === null) {
elPreviousCard = elCard;

} else {
// get the data-card attribute's value from both cards
var card1 = elPreviousCard.getAttribute('data-card');
var card2 = elCard.getAttribute('data-card');
// No match, schedule to flip them back in 1 second
if (card1 !== card2) {

setTimeout(function () {
elCard.classList.remove('flipped');
elPreviousCard.classList.remove('flipped');
elPreviousCard = null;
}, 1000)
wrongPick.play();
} else {
// Yes! a match!
flippedCouplesCount++;
elPreviousCard = null;
rightPick.play();
}
}




i would like to prevent the user from clicking more then two times (it's a memory game.) can someone tell me how can i implement this into my code please?










share|improve this question
















first of all i'm pretty new to JS.
i'm trying to make a memory game but i have the next problem.
the code :



function cardClicked(elCard) {
// If the user clicked an already flipped card - do nothing and return from the function
if (elCard.classList.contains('flipped')) {
return;
}
// Flip it
elCard.classList.add('flipped');

// This is a first card, only keep it in the global variable
if (elPreviousCard === null) {
elPreviousCard = elCard;

} else {
// get the data-card attribute's value from both cards
var card1 = elPreviousCard.getAttribute('data-card');
var card2 = elCard.getAttribute('data-card');
// No match, schedule to flip them back in 1 second
if (card1 !== card2) {

setTimeout(function () {
elCard.classList.remove('flipped');
elPreviousCard.classList.remove('flipped');
elPreviousCard = null;
}, 1000)
wrongPick.play();
} else {
// Yes! a match!
flippedCouplesCount++;
elPreviousCard = null;
rightPick.play();
}
}




i would like to prevent the user from clicking more then two times (it's a memory game.) can someone tell me how can i implement this into my code please?







javascript






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 25 '18 at 0:44









iagowp

1,42411326




1,42411326










asked Nov 25 '18 at 0:19









Free GemsFree Gems

11




11













  • Have you tried preventing the user from clicking twice? What did you try?

    – iagowp
    Nov 25 '18 at 0:24













  • By saying preventing the user from clicking twice i mean clicking on more then 2 cards. (the game goes like that -> you pick a card -> then you pick another card. -> if its a match the cards stays. if not cards fade after 10sec.

    – Free Gems
    Nov 25 '18 at 0:27











  • Can you explain a little better? What scenario are you trying to prevent? Is it when a user has already picked one card, then he immediately picks the second, and a third, or just picking two cards in a small timeframe? What interval should there be between clicks?

    – iagowp
    Nov 25 '18 at 0:30











  • The senario i'm trying to prevent is fast clicking. if the user clicks on 3 cards (quick). he can bug the code. Or prevent the code from giving a delay (the 10 sec delay). But what im trying to achive is after the user clicking 2 times maybe lock all the other cards. until the cards go back to default..

    – Free Gems
    Nov 25 '18 at 0:33





















  • Have you tried preventing the user from clicking twice? What did you try?

    – iagowp
    Nov 25 '18 at 0:24













  • By saying preventing the user from clicking twice i mean clicking on more then 2 cards. (the game goes like that -> you pick a card -> then you pick another card. -> if its a match the cards stays. if not cards fade after 10sec.

    – Free Gems
    Nov 25 '18 at 0:27











  • Can you explain a little better? What scenario are you trying to prevent? Is it when a user has already picked one card, then he immediately picks the second, and a third, or just picking two cards in a small timeframe? What interval should there be between clicks?

    – iagowp
    Nov 25 '18 at 0:30











  • The senario i'm trying to prevent is fast clicking. if the user clicks on 3 cards (quick). he can bug the code. Or prevent the code from giving a delay (the 10 sec delay). But what im trying to achive is after the user clicking 2 times maybe lock all the other cards. until the cards go back to default..

    – Free Gems
    Nov 25 '18 at 0:33



















Have you tried preventing the user from clicking twice? What did you try?

– iagowp
Nov 25 '18 at 0:24







Have you tried preventing the user from clicking twice? What did you try?

– iagowp
Nov 25 '18 at 0:24















By saying preventing the user from clicking twice i mean clicking on more then 2 cards. (the game goes like that -> you pick a card -> then you pick another card. -> if its a match the cards stays. if not cards fade after 10sec.

– Free Gems
Nov 25 '18 at 0:27





By saying preventing the user from clicking twice i mean clicking on more then 2 cards. (the game goes like that -> you pick a card -> then you pick another card. -> if its a match the cards stays. if not cards fade after 10sec.

– Free Gems
Nov 25 '18 at 0:27













Can you explain a little better? What scenario are you trying to prevent? Is it when a user has already picked one card, then he immediately picks the second, and a third, or just picking two cards in a small timeframe? What interval should there be between clicks?

– iagowp
Nov 25 '18 at 0:30





Can you explain a little better? What scenario are you trying to prevent? Is it when a user has already picked one card, then he immediately picks the second, and a third, or just picking two cards in a small timeframe? What interval should there be between clicks?

– iagowp
Nov 25 '18 at 0:30













The senario i'm trying to prevent is fast clicking. if the user clicks on 3 cards (quick). he can bug the code. Or prevent the code from giving a delay (the 10 sec delay). But what im trying to achive is after the user clicking 2 times maybe lock all the other cards. until the cards go back to default..

– Free Gems
Nov 25 '18 at 0:33







The senario i'm trying to prevent is fast clicking. if the user clicks on 3 cards (quick). he can bug the code. Or prevent the code from giving a delay (the 10 sec delay). But what im trying to achive is after the user clicking 2 times maybe lock all the other cards. until the cards go back to default..

– Free Gems
Nov 25 '18 at 0:33














3 Answers
3






active

oldest

votes


















0














You can set a global variable to isClickable initially set to true, enter the code check if its true, if it isn't, return, else go ahead and set it to false and do a timeout to set it to true again, like:



var isClickable = true;
function cardClicked(elCard) {
if (!isClickable) return;
isClickable = false;
setTimeout(function () {
isClickable = true;
}, 500)
...





share|improve this answer































    0














    Since it's a card game I would do the following:
    Make array of elements:



    var cards = (
    {
    "card":"numberOfCard",
    "isclicked" :"yes/no"
    }
    );


    And then every time somebody clicks on card I would change isClickable to no so next time event occures I would prevent it.






    share|improve this answer































      0














      create variable for example opened_cards that store how many card opened, if less than 2 allow open another card else do nothing.



      var opened_cards = 0;

      function cardClicked(elCard) {
      // do check here
      if (opened_cards < 2) {
      // less than 2 card opened do operation
      opened_cards++;
      if (elCard.classList.contains('flipped')) {
      return;
      }
      // Flip it
      elCard.classList.add('flipped');

      // This is a first card, only keep it in the global variable
      if (elPreviousCard === null) {
      elPreviousCard = elCard;

      }
      else {
      // get the data-card attribute's value from both cards
      var card1 = elPreviousCard.getAttribute('data-card');
      var card2 = elCard.getAttribute('data-card');
      // No match, schedule to flip them back in 1 second
      if (card1 !== card2) {

      setTimeout(function() {
      opened_cards -= 2;
      elCard.classList.remove('flipped');
      elPreviousCard.classList.remove('flipped');
      elPreviousCard = null;
      }, 1000);
      wrongPick.play();
      }
      else {
      // Yes! a match!
      opened_cards = 0;
      flippedCouplesCount++;
      elPreviousCard = null;
      rightPick.play();
      }
      }
      }
      }





      share|improve this answer
























      • You should probably put your opened_cards++ after the flipped check

        – iagowp
        Nov 25 '18 at 2:14












      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%2f53463591%2fuser-clicks-too-fast-bug%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      3 Answers
      3






      active

      oldest

      votes








      3 Answers
      3






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      0














      You can set a global variable to isClickable initially set to true, enter the code check if its true, if it isn't, return, else go ahead and set it to false and do a timeout to set it to true again, like:



      var isClickable = true;
      function cardClicked(elCard) {
      if (!isClickable) return;
      isClickable = false;
      setTimeout(function () {
      isClickable = true;
      }, 500)
      ...





      share|improve this answer




























        0














        You can set a global variable to isClickable initially set to true, enter the code check if its true, if it isn't, return, else go ahead and set it to false and do a timeout to set it to true again, like:



        var isClickable = true;
        function cardClicked(elCard) {
        if (!isClickable) return;
        isClickable = false;
        setTimeout(function () {
        isClickable = true;
        }, 500)
        ...





        share|improve this answer


























          0












          0








          0







          You can set a global variable to isClickable initially set to true, enter the code check if its true, if it isn't, return, else go ahead and set it to false and do a timeout to set it to true again, like:



          var isClickable = true;
          function cardClicked(elCard) {
          if (!isClickable) return;
          isClickable = false;
          setTimeout(function () {
          isClickable = true;
          }, 500)
          ...





          share|improve this answer













          You can set a global variable to isClickable initially set to true, enter the code check if its true, if it isn't, return, else go ahead and set it to false and do a timeout to set it to true again, like:



          var isClickable = true;
          function cardClicked(elCard) {
          if (!isClickable) return;
          isClickable = false;
          setTimeout(function () {
          isClickable = true;
          }, 500)
          ...






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 25 '18 at 0:36









          iagowpiagowp

          1,42411326




          1,42411326

























              0














              Since it's a card game I would do the following:
              Make array of elements:



              var cards = (
              {
              "card":"numberOfCard",
              "isclicked" :"yes/no"
              }
              );


              And then every time somebody clicks on card I would change isClickable to no so next time event occures I would prevent it.






              share|improve this answer




























                0














                Since it's a card game I would do the following:
                Make array of elements:



                var cards = (
                {
                "card":"numberOfCard",
                "isclicked" :"yes/no"
                }
                );


                And then every time somebody clicks on card I would change isClickable to no so next time event occures I would prevent it.






                share|improve this answer


























                  0












                  0








                  0







                  Since it's a card game I would do the following:
                  Make array of elements:



                  var cards = (
                  {
                  "card":"numberOfCard",
                  "isclicked" :"yes/no"
                  }
                  );


                  And then every time somebody clicks on card I would change isClickable to no so next time event occures I would prevent it.






                  share|improve this answer













                  Since it's a card game I would do the following:
                  Make array of elements:



                  var cards = (
                  {
                  "card":"numberOfCard",
                  "isclicked" :"yes/no"
                  }
                  );


                  And then every time somebody clicks on card I would change isClickable to no so next time event occures I would prevent it.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 25 '18 at 0:39









                  Nikola PavlovićNikola Pavlović

                  11115




                  11115























                      0














                      create variable for example opened_cards that store how many card opened, if less than 2 allow open another card else do nothing.



                      var opened_cards = 0;

                      function cardClicked(elCard) {
                      // do check here
                      if (opened_cards < 2) {
                      // less than 2 card opened do operation
                      opened_cards++;
                      if (elCard.classList.contains('flipped')) {
                      return;
                      }
                      // Flip it
                      elCard.classList.add('flipped');

                      // This is a first card, only keep it in the global variable
                      if (elPreviousCard === null) {
                      elPreviousCard = elCard;

                      }
                      else {
                      // get the data-card attribute's value from both cards
                      var card1 = elPreviousCard.getAttribute('data-card');
                      var card2 = elCard.getAttribute('data-card');
                      // No match, schedule to flip them back in 1 second
                      if (card1 !== card2) {

                      setTimeout(function() {
                      opened_cards -= 2;
                      elCard.classList.remove('flipped');
                      elPreviousCard.classList.remove('flipped');
                      elPreviousCard = null;
                      }, 1000);
                      wrongPick.play();
                      }
                      else {
                      // Yes! a match!
                      opened_cards = 0;
                      flippedCouplesCount++;
                      elPreviousCard = null;
                      rightPick.play();
                      }
                      }
                      }
                      }





                      share|improve this answer
























                      • You should probably put your opened_cards++ after the flipped check

                        – iagowp
                        Nov 25 '18 at 2:14
















                      0














                      create variable for example opened_cards that store how many card opened, if less than 2 allow open another card else do nothing.



                      var opened_cards = 0;

                      function cardClicked(elCard) {
                      // do check here
                      if (opened_cards < 2) {
                      // less than 2 card opened do operation
                      opened_cards++;
                      if (elCard.classList.contains('flipped')) {
                      return;
                      }
                      // Flip it
                      elCard.classList.add('flipped');

                      // This is a first card, only keep it in the global variable
                      if (elPreviousCard === null) {
                      elPreviousCard = elCard;

                      }
                      else {
                      // get the data-card attribute's value from both cards
                      var card1 = elPreviousCard.getAttribute('data-card');
                      var card2 = elCard.getAttribute('data-card');
                      // No match, schedule to flip them back in 1 second
                      if (card1 !== card2) {

                      setTimeout(function() {
                      opened_cards -= 2;
                      elCard.classList.remove('flipped');
                      elPreviousCard.classList.remove('flipped');
                      elPreviousCard = null;
                      }, 1000);
                      wrongPick.play();
                      }
                      else {
                      // Yes! a match!
                      opened_cards = 0;
                      flippedCouplesCount++;
                      elPreviousCard = null;
                      rightPick.play();
                      }
                      }
                      }
                      }





                      share|improve this answer
























                      • You should probably put your opened_cards++ after the flipped check

                        – iagowp
                        Nov 25 '18 at 2:14














                      0












                      0








                      0







                      create variable for example opened_cards that store how many card opened, if less than 2 allow open another card else do nothing.



                      var opened_cards = 0;

                      function cardClicked(elCard) {
                      // do check here
                      if (opened_cards < 2) {
                      // less than 2 card opened do operation
                      opened_cards++;
                      if (elCard.classList.contains('flipped')) {
                      return;
                      }
                      // Flip it
                      elCard.classList.add('flipped');

                      // This is a first card, only keep it in the global variable
                      if (elPreviousCard === null) {
                      elPreviousCard = elCard;

                      }
                      else {
                      // get the data-card attribute's value from both cards
                      var card1 = elPreviousCard.getAttribute('data-card');
                      var card2 = elCard.getAttribute('data-card');
                      // No match, schedule to flip them back in 1 second
                      if (card1 !== card2) {

                      setTimeout(function() {
                      opened_cards -= 2;
                      elCard.classList.remove('flipped');
                      elPreviousCard.classList.remove('flipped');
                      elPreviousCard = null;
                      }, 1000);
                      wrongPick.play();
                      }
                      else {
                      // Yes! a match!
                      opened_cards = 0;
                      flippedCouplesCount++;
                      elPreviousCard = null;
                      rightPick.play();
                      }
                      }
                      }
                      }





                      share|improve this answer













                      create variable for example opened_cards that store how many card opened, if less than 2 allow open another card else do nothing.



                      var opened_cards = 0;

                      function cardClicked(elCard) {
                      // do check here
                      if (opened_cards < 2) {
                      // less than 2 card opened do operation
                      opened_cards++;
                      if (elCard.classList.contains('flipped')) {
                      return;
                      }
                      // Flip it
                      elCard.classList.add('flipped');

                      // This is a first card, only keep it in the global variable
                      if (elPreviousCard === null) {
                      elPreviousCard = elCard;

                      }
                      else {
                      // get the data-card attribute's value from both cards
                      var card1 = elPreviousCard.getAttribute('data-card');
                      var card2 = elCard.getAttribute('data-card');
                      // No match, schedule to flip them back in 1 second
                      if (card1 !== card2) {

                      setTimeout(function() {
                      opened_cards -= 2;
                      elCard.classList.remove('flipped');
                      elPreviousCard.classList.remove('flipped');
                      elPreviousCard = null;
                      }, 1000);
                      wrongPick.play();
                      }
                      else {
                      // Yes! a match!
                      opened_cards = 0;
                      flippedCouplesCount++;
                      elPreviousCard = null;
                      rightPick.play();
                      }
                      }
                      }
                      }






                      share|improve this answer












                      share|improve this answer



                      share|improve this answer










                      answered Nov 25 '18 at 1:11









                      ewwinkewwink

                      12.3k22441




                      12.3k22441













                      • You should probably put your opened_cards++ after the flipped check

                        – iagowp
                        Nov 25 '18 at 2:14



















                      • You should probably put your opened_cards++ after the flipped check

                        – iagowp
                        Nov 25 '18 at 2:14

















                      You should probably put your opened_cards++ after the flipped check

                      – iagowp
                      Nov 25 '18 at 2:14





                      You should probably put your opened_cards++ after the flipped check

                      – iagowp
                      Nov 25 '18 at 2:14


















                      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%2f53463591%2fuser-clicks-too-fast-bug%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







                      這個網誌中的熱門文章

                      Xamarin.form Move up view when keyboard appear

                      Post-Redirect-Get with Spring WebFlux and Thymeleaf

                      Anylogic : not able to use stopDelay()