JavaScript只在中断的调试模式下工作

JavaScript只在中断的调试模式下工作,javascript,Javascript,我有一个JavaScript代码,应该将输入框添加到html中,然后将用户输入保存到和数组中。当我定期运行代码时,屏幕会刷新,但什么也没发生。如果我再次运行它,它应该如何工作。类似地,如果我打开调试模式并在代码中设置一个断点,那么在我第一次尝试运行它时就可以正常工作。是否存在我不知道的异步问题 此外,事件侦听器正在检查是否有人在网页上按enter键,否则代码不会执行 另外,我还不熟悉stack overflow,我发现到目前为止帮助很大,如果我没有按照适当的礼仪提问,请让我知道 var reci

我有一个JavaScript代码,应该将输入框添加到html中,然后将用户输入保存到和数组中。当我定期运行代码时,屏幕会刷新,但什么也没发生。如果我再次运行它,它应该如何工作。类似地,如果我打开调试模式并在代码中设置一个断点,那么在我第一次尝试运行它时就可以正常工作。是否存在我不知道的异步问题

此外,事件侦听器正在检查是否有人在网页上按enter键,否则代码不会执行

另外,我还不熟悉stack overflow,我发现到目前为止帮助很大,如果我没有按照适当的礼仪提问,请让我知道

var recipients = document.getElementById("howMany");
recipients.addEventListener("keydown", function (e) {
    if (e.keyCode === 13) {
        validate(e);
    }
})

 function validate(e) {
    var num2 = document.getElementById("howMany");
    num2 = document.getElementById("howMany").value;
    var values = new Array();
  for (i = 1; i < num2; i++){
                    //loop set to repeat to the amount of guests inputed by user
                    container.appendChild(document.createTextNode("Recipient " + (i+1)+":"));
                        //creates a text for the user to read which recipient each input is for
                    var x = document.createElement("input");
                        //creates the input element
                    x.setAttribute("id", "empInput"+i);
                    x.setAttribute("type", "text");
                    x.setAttribute("value", "");
                        //sets the type and attribute
                    container.appendChild(x);
                        //places the input element in the container
                    values[i] = document.getElementById("empInput"+i).value;

        } 

    //console.log(values);
 }
var recipients=document.getElementById(“多少”);
recipients.addEventListener(“向下键”,函数(e){
如果(如keyCode===13){
验证(e);
}
})
功能验证(e){
var num2=document.getElementById(“多少”);
num2=document.getElementById(“多少”).value;
var值=新数组();
对于(i=1;i
我不知道这段代码在html中的什么位置,但是如果脚本在
加载完多少
元素之前执行,那么初始
getElementById
可能会失败。最好在
onpageload
事件中执行。setTimeout闻起来很有趣。你为什么要使用它?我删除了setTimeout,我想可能需要先加载DOM。如果你发布
html
too,这会很有帮助。如果它是一个表单,使用Enter键提交,只需添加
e.preventDefault()内部
if
以避免表单提交。