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;
}
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
add a comment |
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
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
add a comment |
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
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
javascript
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
add a comment |
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
add a comment |
3 Answers
3
active
oldest
votes
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)
...
add a comment |
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.
add a comment |
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();
}
}
}
}
You should probably put your opened_cards++ after the flipped check
– iagowp
Nov 25 '18 at 2:14
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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)
...
add a comment |
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)
...
add a comment |
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)
...
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)
...
answered Nov 25 '18 at 0:36
iagowpiagowp
1,42411326
1,42411326
add a comment |
add a comment |
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.
add a comment |
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.
add a comment |
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.
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.
answered Nov 25 '18 at 0:39
Nikola PavlovićNikola Pavlović
11115
11115
add a comment |
add a comment |
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();
}
}
}
}
You should probably put your opened_cards++ after the flipped check
– iagowp
Nov 25 '18 at 2:14
add a comment |
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();
}
}
}
}
You should probably put your opened_cards++ after the flipped check
– iagowp
Nov 25 '18 at 2:14
add a comment |
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();
}
}
}
}
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();
}
}
}
}
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
add a comment |
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
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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