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 pletely 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
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 pletely 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
Share Improve this question edited May 23, 2017 at 12:26 CommunityBot 11 silver badge asked May 20, 2014 at 8:25 user2546845user2546845 2- See here stackoverflow./questions/1451009/… – elclanrs Commented May 20, 2014 at 8:26
- If you are adding listeners in a loop you probably doing something wrong. What's the motivation behind this? – J0HN Commented May 20, 2014 at 8:33
1 Answer
Reset to default 4There is no way to pause a function in JavaScript. You need to pletely 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.