javascript 暂停功能直到按下回车键javascript

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17176046/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 07:23:04  来源:igfitidea点击:

pause function until enter key is pressed javascript

javascriptfunction

提问by Noel Franklin

new to javascript. i know this might be really simple but I can't figure it out. I want to execute a function. Pause in the middle of the function and wait for the user to hit “enter” key which will allow function to continue again (or will call another function to fire).

JavaScript 新手。我知道这可能很简单,但我无法弄清楚。我想执行一个函数。在函数中间暂停并等待用户点击“回车”键,这将允许函数再次继续(或将调用另一个函数来触发)。

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";


}

回答by Smeegs

I would create a global variable to see if the javascript is waiting for a key press.

我会创建一个全局变量来查看 javascript 是否正在等待按键。

At the top of your script you can add

在脚本的顶部,您可以添加

var waitingForEnter = false;

Then set it to true in your function

然后在您的函数中将其设置为 true

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);
}

I hope this helps. This is just what I would do, it might not be the best approach.

我希望这有帮助。这只是我会做的,它可能不是最好的方法。

回答by Jacob

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

或者我们可以从 Javalsu 内联解决方案,并摆脱全局变量。

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);
    }
}