javascript - Wait for click in loop - Stack Overflow

Before even starting, I know there already has been a thread about this, but unfortunately it did not h

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
Add a ment  | 

1 Answer 1

Reset to default 4

There 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.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745312884a4622081.html

相关推荐

  • javascript - Wait for click in loop - Stack Overflow

    Before even starting, I know there already has been a thread about this, but unfortunately it did not h

    4小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信