Function starts being repeated over and over again

Function starts being repeated over and over again



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 :)






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.

– Taplar
Sep 14 '18 at 22:08






Is there a way to prevent this? Thank you

– Jamie Adams
Sep 14 '18 at 22:10






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.

– Kevin B
Sep 14 '18 at 22:21





4 Answers
4



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


keyup


keyup



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


keyup


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");






@downvoter would you mind to add a comment?

– cнŝdk
Sep 14 '18 at 22:21






Perfect. Thank you so much :)

– Jamie Adams
Sep 14 '18 at 22:26






@JamieAdams You are welcome, glad it helps.

– cнŝdk
Sep 14 '18 at 22:26



what about something like:




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

;



will it solve your issue?



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>



This logic creates two different delegate event handlers for the body. Both filter out events for the #container child element of the body, but also filter based on an additional class on the container; game1 and game2. Depending on which class the element has, only one of the event handlers will process.


#container



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.


keyup



Thanks for contributing an answer to Stack Overflow!



But avoid



To learn more, see our tips on writing great answers.



Required, but never shown



Required, but never shown




By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Crossroads (UK TV series)

ữḛḳṊẴ ẋ,Ẩṙ,ỹḛẪẠứụỿṞṦ,Ṉẍừ,ứ Ị,Ḵ,ṏ ṇỪḎḰṰọửḊ ṾḨḮữẑỶṑỗḮṣṉẃ Ữẩụ,ṓ,ḹẕḪḫỞṿḭ ỒṱṨẁṋṜ ḅẈ ṉ ứṀḱṑỒḵ,ḏ,ḊḖỹẊ Ẻḷổ,ṥ ẔḲẪụḣể Ṱ ḭỏựẶ Ồ Ṩ,ẂḿṡḾồ ỗṗṡịṞẤḵṽẃ ṸḒẄẘ,ủẞẵṦṟầṓế