Function starts being repeated over and over again

iterated function example
if you hear something enough times you start to believe it
iterated function calculator
iterative function
how are iteration and composition functions related brainly
when does a lie become the truth
if you say something often enough it becomes the truth
when lies become the truth quotes

I've been developing a simple system that is supposed to change between two different scenes when you press a button.

gameOne();

var game = 1;

function gameOne() {
  game = 1;
  console.log("Game 1");
  $( "body" ).keyup(function( event ) {
        if ( event.which == 49 && game == 1) { // Number 1 key
           gameTwo(); 
        }
  });
}

function gameTwo() {
  game = 2;
  console.log("Game 2");
  $( "body" ).keyup(function( event ) {
    if ( event.which == 49 && game == 2) { // Number 1 key
        gameOne();
    } 
  });
}

Expected behaviour - I want it to say Game 1, after after pressing the 1 key and then Game 2 after pressing the 1 key again, and then repeat this as I press 1.

Actual behaviour - It does the expected behaviour a few times, and then it starts repeating 1 and 2 over and over again, to the point it lags the browser out.

JSFiddle: https://jsfiddle.net/a0npotm8/10/

I'm really sorry if this is a basic question or anything, I'm still fairly new to Javascript and JQuery and this is really confusing me currently.

All help is appreciated. Thank you :)

The problem here is that you are rebinding the keyup event recuresively inside the keyup callback, so it ends up by breaking the browser.

What you need to do is to get the keyup binding code out of the two functions:

gameOne();

var game = 1;

$("body").keyup(function(event) {
  if (event.which == 49 && game == 1) { // Number 1 key
    gameTwo();
  } else if (event.which == 49 && game == 2) { // Number 1 key
    gameOne();
  }
});


function gameOne() {
  game = 1;
  console.log("Game 1");
}

function gameTwo() {
  game = 2;
  console.log("Game 2");
}

javascript, The problem here is that you are rebinding the keyup event recuresively inside the keyup callback, so it ends up by breaking the browser. OCD is a common, chronic (long-lasting) disorder in which a person has uncontrollable, reoccurring thoughts (obsessions) and behaviors (compulsions) that he or she feels the urge to repeat over and over in response to the obsession.

what about something like:

let game = 1;
document.onkeyup = ev => {
  if (ev.which === 49) {
    console.log(`Game ${game}`);
    game = game === 1 ? 2 : 1;
  }
};

Learn Raspberry Pi Programming with Python: Learn to Program on , We can create a list at the beginning of the script, after the import statements, and many times should make you think of one element of coding: a function—also in code if there's a process you're going to be repeating over and over again. Functions . Functions are "self contained" modules of code that accomplish a specific task. Functions usually "take in" data, process it, and "return" a result. Once a function is written, it can be used over and over and over again. Functions can be "called" from the inside of other functions.

You can use a delegate event handler to control actions like this, so you do not have to juggle event bindings around.

var $container = $('#container').focus();

$(document.body)
  .on('keyup', '#container.game1', function(e){
    if (e.which == 49) {
      console.log('Game 1');
      $container.removeClass('game1').addClass('game2');
    }
  })
  .on('keyup', '#container.game2', function(e){
    if (e.which == 49) {
      console.log('Game 2');
      $container.removeClass('game2').addClass('game1');
    }
  });
#container {
  min-width: 100vw;
  min-height: 100vh;
  background-color: rgb(128, 128, 128);
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="container" class="game1" tabindex="0">
</div>

Pre-Calculus For Dummies, draw the graph to show one period of the function, because in this period you capture all possible values for sine before it starts repeating over and over again​  Nightmares from time to time are one thing, but if you find yourself having the same one over and over again, Ellis says you should contact a medical expert for help.

Whenever you call keyup on an element, you attach another event handler. To catch events, you only need to call it once. The callback functions that handle the event will fire every time the event happens.

Intelligent Computer Systems in Engineering Design: Principles and , draw the graph to show one period of the function, because in this period you capture all possible values for sine before it starts repeating over and over again​  When we repeat a word over and over again, it starts to sound very weird and become only a bunch of repeating sounds. You don't even need to repeat it quickly. Why does a word repeated many times lose its meaning and the brain can't recognize it properly any more?

Over the Rainbow: The Wizard of Oz as a Secular Myth of America, For a single objective problem: What property or function should be optimised (​e.g. The principle of hill climbing as an optimisation method implies starting at a This is the criteria for the optimum being found and the search should terminate. This process is repeated over and over again 107 7.3 Optimisation 7.3.1 Hill  In every undistracted moment you find your mind replaying the unwanted thought, concept, melody, or song over and over again. No matter what you do, you seemingly can't stop your mind from thinking of the stuck thought over and over again. Even after a good sleep, you wake up and the stuck thought starts to play all over again.

Analytic Cyclopedia of Practical Medicine, To be fully alive, to be truly human, even to be completely real, men and the way things were in the Beginning, to start over again; time is thus reversible, or cyclical, since these primordial events are repeated over and over again. They are camouflaged— or, if you will, "fallen"— but they continue to perform their function. On the other hand, if you have a pure function, which executes no side effects, you could use either approach, but the first would only result in one application of the function, which could be significantly cheaper.

Nature, There are also cases in which the movements are tic-like from the beginning, and it is closely related to' psychic functions, while the choreic movement, on the other the movements of tic are co-ordinated and repeated over and over again. beginning and in mild cases they are apt to be limited to one side of the body. President Trump is a "great businessman," he says over and over again. Some evidence suggests that might not be true. Or look at just this week, when the president signed three executive orders

Comments
  • Every time you call either method, you are creating a new keyup event handler on the body. That's why you are seeing repetitive operations.
  • Is there a way to prevent this? Thank you
  • Don't bind key events inside key events. inside key events. inside key events. inside key events. inside key events. inside key events. inside key events. inside key events. inside key events.
  • Perfect. Thank you so much :)