JavaScript Lagging Javascript Card Game

I’m trying to make a card game in javascript. I want the user to see the front of a card when clicking the button ”clicky” and then the back of the same card when clicking again. On the click after that I want it to switch to the front of the next card and so on. As it is now I reload the page with five new cards when the fifth card has been shown. Each card is an item in the variable cards and the element that changes with each click is the p tag with the id card.

My code is working but if I have more cards, about seven, it starts lagging. Is it a bad idea to have two functions that calls each other like this? Is there a better way to write this code so that I can have more cards?

Here's the code:

<html>
<body>
<p id=”card”></p>
<button id="clicky">Fråga</button>
<script type="text/javascript">
    var cards = [[”front1”, ”back1”], [”front2”, ”back2”], [”front3”, ”back3”], [”front4”, ”back4”], [”front5”, ”back5”]];

    var a = 0;
    var clicky = document.getElementById("clicky");
    clicky.addEventListener("click", function(){setFront(a)});

    var card = document.getElementById(”card”);

    function setFront (a) {
        card.textContent = cards[a][0];
        clicky.addEventListener("click", function(){setBack(a)});
    }

    function setBack (a) {
        card.textContent = cards[a][1];
        a += 1;
        if (a < 5) {
            clicky.addEventListener("click", function(){setFront(a)});
        } else {
            clicky.addEventListener("click", function() {
                window.location=url;
            });
        }
    }
</script>
</body>
</html>

Any help appreciated!

Answer:1

I've got a list of items, each with their own checkboxes and I've decided to try add a 'select all' checkbox to make it easier for the user to select them all at once. Unfortunately I'm finding it ...

I've got a list of items, each with their own checkboxes and I've decided to try add a 'select all' checkbox to make it easier for the user to select them all at once. Unfortunately I'm finding it ...

  1. react checkbox select all
  2. react checkbox select only one
  3. react select checkbox example
  4. react select checkbox dropdown
  5. react checkbox checked
  6. react checkbox checked attribute
  7. react checkbox checked not working
  8. react checkbox checked state
  9. react checkbox checked value
  10. react checkbox checked not updating
  11. react checkbox checked event
  12. react checkbox checked onchange
  13. react checkbox checked by default
  14. react checkbox checked and unchecked
  15. react checkbox checked function
  16. react checkbox checked array
  17. react checkbox checked change
  18. react checkbox checked property
  19. react checkbox checked css
  20. react checkbox checked or not

I have following string. var str = "abc, abcd, abc, abcf, abc, abc"; I want to remove abc from a given string with , (space). Basically, I want output something like below. var output = "abcd, abcf"...

I have following string. var str = "abc, abcd, abc, abcf, abc, abc"; I want to remove abc from a given string with , (space). Basically, I want output something like below. var output = "abcd, abcf"...

  1. remove word with regex
  2. remove word with sed
  3. remove word from string python
  4. remove word from string javascript
  5. remove word from string php
  6. remove word from string java
  7. remove word from string c#
  8. remove word from excel cell
  9. remove word from string
  10. remove word from list python
  11. remove word in excel
  12. remove word from string js
  13. remove word from google search
  14. remove word from iphone dictionary
  15. remove word from dictionary chrome
  16. remove word from autocorrect iphone
  17. remove word from string jquery
  18. remove word from autocorrect
  19. remove word from string bash
  20. remove word from dictionary

I got an object which looks like this : { "a": "string not empty", "b": { "c": "string not empty", }, "d": { "e": false, "f": 0, "g": true, ...

I got an object which looks like this : { "a": "string not empty", "b": { "c": "string not empty", }, "d": { "e": false, "f": 0, "g": true, ...

I am following a tutorial in order to perform Asynchronous validation in Angular. What I am trying to achieve is my custom validator which is shouldBeUnique should be call after delay of 2 seconds. I ...

I am following a tutorial in order to perform Asynchronous validation in Angular. What I am trying to achieve is my custom validator which is shouldBeUnique should be call after delay of 2 seconds. I ...

  1. angular settimeout function
  2. angularjs settimeout function
  3. angular timeout function
  4. angular timeout function example
  5. angular timeout function with parameters
  6. angularjs timeout function
  7. angularjs timeout function with parameter
  8. angularjs timeout function example
  9. angular 6 settimeout function
  10. angular 4 settimeout function
  11. angular 5 settimeout function
  12. angularjs timeout call function
  13. angular 7 settimeout function
  14. angular settimeout arrow function
  15. settimeout function angular 2
  16. settimeout function angularjs
  17. angularjs settimeout inside function