title: Wait for callback before continue for loop
body: <p>I've a for-loop I'm looping through.</p>
  
  <p>I want to make a custom modal and wait for a response before continue it.</p>
  
  <p>How can I achieve this? I know I've to wait for a callback.</p>
  
  <p>Like this example:</p>
  
  <pre><code> for(var x in array){
              alert(x);
              console.log(x);
          }
  </code></pre>
  
  <p>It does exactly what I want to. But I want to have three buttons.
  But alert is not part of javascript(? It's in the browser.)</p>
  
  <p>So, do you guys have an idea?</p>
  
  <p>I was thinking about doing something like this:</p>
  
  <pre><code>var run = true;
  function foo(){
      if (run){
          setTimeout(foo, 500);
      }
  }
  
  function stop(){
      run = false;
  }
  
  foo();
  </code></pre>
  
  <p>and then wait for a stop which calls on a button click before continue. But is this really good practice?</p>
  
  <p>Or use a lambda function as a parameter to the customAlert and a "global" variable that holds the current position of the array I'm going through and do this with functions. Like: Check if array is still holding keys greater than X.
  Then do the function again and each time increase the global X.</p>
  
  <p>Thank you lostsource for the code:
  Oh, I got an idea; I'll simply use lostsource's solution inside an anonymous function, so I don't get global variables. Excellent.</p>
  
  <pre><code>(function(){
  
  })();
  </code></pre>