0

I am running a loop and I want a jQuery function to happen before the loop continues. here is an example.

var battle=true;

while(battle){
 $('.action1').click(n=0);
 $('.action2').click(n=1);
 $('.action3').click(n=2);
 $('.action4').click(n=3);

action[n]
etc....
}

I am currently making a text based rpg game. Whenever user input was needed I would use a prompt() and whenever user information was given I would use an alert().

is there some way for me to have the while loop progress through receiving user input and giving input result by the user clicking on html elements?

VioleBaam
  • 3
  • 1

1 Answers1

0

just make a function call from the loop like this:

function battle(){
    var battle=true;

    while(battle){
         $('.action1').click(n=0);
         $('.action2').click(n=1);
         $('.action3').click(n=2);
         $('.action4').click(n=3);

        myFunction();
    }
}


function myFunction(){
    //do somethhing
}

if you want to stop the loop just use the keyword:

break;

If you want pause execution waiting for user input read this stackoverflow question: JavaScript pausing execution of function to wait for user input

Community
  • 1
  • 1
MGot90
  • 2,422
  • 4
  • 15
  • 31
  • I probably should have mentioned that my code is kind of long. – VioleBaam Mar 11 '14 at 21:35
  • @VioleBaam that means nothing at all! Did you use this? if so please thumbs up so I get credit ... if not tell me why and I'll correct it. – MGot90 Mar 11 '14 at 21:36
  • I have multiple instances where user input is needed and information needs to be shown to the user do I need to make all cases a loop? not to be rude but I don't really see how this solution helps. – VioleBaam Mar 11 '14 at 21:38