最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Wait for click in loop - Stack Overflow

programmeradmin1浏览0评论

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.

发布评论

评论列表(0)

  1. 暂无评论