1

I want the function to when it is called, will:

  • Set up the input element(done)
  • Pause the execution until the user presses the enter key
  • Set down the input element(done)
  • Return the user input from as the function return value

This is for the program is reading a string and executing stuff based on the string sequentially to the string, and I want to change the behavior of the program by prompting the user input at some point. The reading and executing must also stop while the prompt.

The program is completely synchronous.

It is also not a node.js program. It is to be run on a browser and to be able to run offline.

I have an input element.

<input type="text" id="inputElement" readonly>

And I have the function which calls the function which lets the user put value into the element.

function getUserInput(){
  var e=document.getElementById("inputElement");
  e.value="";
  e.readOnly=false;

  //TODO:
  //pause execution and assign the user input to the variable x

  e.readOnly=true;
  return x;
}

And a function that calls getUserInput.

function foo(){
  var a=getUserInput();
  //do something
}

I have not been able to figure out how to stop the execution of the program while also not crashing it, and also returning the value from the function and not, say just assign it to a variable.

Naruyoko
  • 153
  • 7
  • 2
    You don't "pause" things in JS. There is only one thread, if you were able to pause it, literally _everything_ would stop. Instead, rely on the fact that JS events simply trigger the right code: when the user clicks a HTML element, make that event run the code that needs to run "once the user clicks". – Mike 'Pomax' Kamermans Aug 13 '19 at 23:55
  • Stimulus—response, stimulus—response… there is no pause. – RobG Aug 14 '19 at 00:01
  • Possible duplicate of [How do I return the response from an asynchronous call?](https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call); see [this answer](https://stackoverflow.com/a/34959258/1026) in particular. – Nickolay Aug 14 '19 at 17:29

0 Answers0