2

Before even starting, I know there already has been a thread about this, but unfortunately it did not help me at all.

So here is my problem, I have a loop written in JavaScript and at the end of it is a button click event. The event is related to a button situated inside a popup window.

for(var i=0; i<value; i++){
  [...]
  //some code here
  [...]
  //opens the window
  windowButton.addEventListener('click', function(){
    //code
  });
  //here I would like for it to continue once the click has been triggered
}

Unfortunately, it doesn't wait for the click.

Like said in the similar post, incrementing the variable i inside the function doesn't work, or even using a global variable. And the suggested answers are not what I am looking for.

[EDIT] Okay, so I'm going to add some information to be more precise. I need to create a form. But it also needs to be able to parse a file containing all the information, and to be able to fill it. For each line of information of the file, so each time the form is completely filled, a window needs to open and wait for the validate button situated inside it.

Si I am hoping I made myself clear enough. [/EDIT]

Thank you in advance for any reponse

Community
  • 1
  • 1
  • See here http://stackoverflow.com/questions/1451009/javascript-infamous-loop-issue – elclanrs May 20 '14 at 08:26
  • If you are adding listeners in a loop you probably doing something wrong. What's the motivation behind this? – J0HN May 20 '14 at 08:33

1 Answers1

4

There is no way to pause a function in JavaScript. You need to completely change your approach.

Move the code that you currently run each time you go around the loop into a separate function.

Create a variable outside that function.

Each time the function is called, increment that variable.

If the variable is "too big" return from the function before doing anything.

Assign that function as your click event handler.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
  • So I tried, but I'm not sure how I'm going to able to achieve what you answered, if I understand well you're talking about recursion ? which I already tried, but in my case is impossible since the recursion recalls back the whole stack, thanks again –  May 20 '14 at 09:33
  • Yes, you need to use recursion for this. I've no idea what "recalls back the whole stack" means or why it would prevent you from doing what you describe in the question. – Quentin May 20 '14 at 09:35
  • Well, when using the recursion, if I'm not wrong, each time the function is called it is added to the stack/pile. When the function finished calling itself all the functions contained in the stack, are then recalled. I've tested, and the function, when called once is then recalled. If I'm wrong, please let me know, Thanks again –  May 20 '14 at 09:46
  • You're wrong. With normal recursion, the function will run until the point where it calls itself again. When that function returns, the previous one will continue running from the point it left off. In this case, the function isn't really calling itself, it is setting up an event handler that will call the function when the event fires. But in either case, the function won't be called in both directions. – Quentin May 20 '14 at 09:53
  • Well, I had made a mistake, and it works, so thank you again for all your help –  May 20 '14 at 12:02
  • What is meant by "If the variable is too big"? – 2540625 May 06 '19 at 02:38
  • @jtheletter — if `i – Quentin May 06 '19 at 08:02