Pause until javascript enter key is pressed

new for javascript. I know that it can be very simple, but I cannot understand it. I want to execute a function. Pause in the middle of the function and wait until the user presses the "enter" key, which will allow him to continue working (or call another function to start).

function appear() { document.getElementById("firstt").style.visibility="visible"; //here is where I want the pause to happen until the user presses "enter" key //Below is what I want to happen after the "enter" key has been pressed. document.getElementById("startrouter").style.visibility="visible"; } 
+4
source share
2 answers

I would create a global variable to see if javascript is waiting for a keystroke.

At the top of the script you can add

 var waitingForEnter = false; 

Then set to true in your function

 function appear() { document.getElementById("firstt").style.visibility="visible"; waitingForEnter = true; } 

Then ... add a listener for the enter key

 function keydownHandler(e) { if (e.keyCode == 13 && waitingForEnter) { // 13 is the enter key document.getElementById("startrouter").style.visibility="visible"; waitingForEnter = false; // reset variable } } // register your handler method for the keydown event if (document.addEventListener) { document.addEventListener('keydown', keydownHandler, false); } else if (document.attachEvent) { document.attachEvent('onkeydown', keydownHandler); } 

Hope this helps. This is what I would do, it may not be the best approach.

+3
source

or we can embed the solution from Javalsu and get rid of the global variable.

 function appear(){ document.getElementById("firstt").style.visibility="visible"; //here is where I want the pause to happen until the user presses "enter" key function after(){ //Below is what I want to happen after the "enter" key has been pressed. document.getElementById("startrouter").style.visibility="visible"; } function keydownHandler(e) { if (e.keyCode == 13 && waitingForEnter) { // 13 is the enter key after(); } } // register your handler method for the keydown event if (document.addEventListener) { document.addEventListener('keydown', keydownHandler, false); } else if (document.attachEvent) { document.attachEvent('onkeydown', keydownHandler); } } 
+1
source

Source: https://habr.com/ru/post/1486894/


All Articles