Q:

Javascript code not running sequentially

Halcyon helped with the loop here - but is there any way to stop the subsequent code from running, until after the loop has finished:
When I run the code below, I get the prompt for entering my name, before the scrollDown function runs - but the input prompt as AFTER the scrollDown code:

function scrollDown(num_times) {
  num_times -= 1;
  if (num_times === 0) {
    return;
  }
  window.scrollBy(0, 500); // horizontal and vertical scroll increments
  setTimeout(function() {
    scrollDown(num_times);
  }, 500);
}
//This should run first and scroll the screen before prompting
scrollDown(30); // scroll down 30 times


//However this prompt comes up before the above code has ran
var kw = prompt("Please enter your name");

Any advice would be appreciated. 
Thanks, Mark

A:

var kw;

function scrollDown(num_times) {
  if (num_times === 0) {
    /* you can define your prompt after scrollDown has ended */
    kw = prompt("Please enter your name");
    return;
  }
  window.scrollBy(0, 500);
  setTimeout(function() {
    scrollDown(num_times - 1);
  }, 500);
}

scrollDown(30);

