暂停功能直到输入键被按下javascript [英] pause function until enter key is pressed javascript

查看:94
本文介绍了暂停功能直到输入键被按下javascript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

javascript新手。我知道这可能很简单,但我无法理解。我想执行一个函数。在函数中间暂停并等待用户点击输入键,这将允许函数再次继续(或将调用另一个函数来触发)。

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).

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


}


推荐答案

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

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

在脚本的顶部你可以添加

At the top of your script you can add

var waitingForEnter = false;

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

Then set it 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);
}

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

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

这篇关于暂停功能直到输入键被按下javascript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆