新手到javascript.我知道这可能很简单,但我无法理解.我想执行一个函数.在函数中间暂停并等待用户点击“enter”键,这将允许函数再次继续(或将调用另一个函数来触发).
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是否在等待按键.
原文链接:https://www.f2er.com/js/429773.html在脚本的顶部,您可以添加
var waitingForEnter = false;
然后在函数中将其设置为true
function appear()
{
document.getElementById("firstt").style.visibility="visible";
waitingForEnter = true;
}
然后……为回车键添加一个监听器
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);
}
我希望这有帮助.这正是我要做的,它可能不是最好的方法.