我需要删除我的 Javascript 程序最终输出中出现的循环

问题描述 投票:0回答:1

我正在编写一个程序,可以检测用户在特定秒或分钟内可以输入的字数。我是 javascript 的新手,我的理解很基础。这里的问题是,在成功计算字数后,'time limit exceeded' 警告循环显示并且等于键入的字数。控制台中显示的最终结果输出也会发生同样的事情。所以可以肯定的是,循环与输入的数量有关。请帮忙

我已经尝试过切换代码,改变位置并研究了循环的原因,但我似乎卡住了并决定尝试 stackoverflow

Javascript脚本代码:

    let no_of_words =''

const input_box = document.getElementById('input_box')
const input_para = document.getElementById('input_para')
const input_submit = document.getElementById('input_submit')


function input_result() {
  const info = document.createElement('p')
  info.textContent ='You have exceeded the time'
  info.style.color ='red'
  input_para.appendChild(info)
  //console.log(boxer);
  no_of_words = boxer.length;
  console.log(`You succesfully typed ${no_of_words} words in 10 seconds`);

}

function lock_input(){
  input_box.style.visibility = 'hidden'
  input_result()

}



const boxer =[]



input_box.addEventListener('keydown', (event)=>{
  setTimeout(lock_input, 3000);
  //console.log(`You pressed "${event.key}".`)
  boxer.push(event.key)
  

});

HTML代码:

 <input type="textbox" id="input_box">
<input type="submit" id="input_submit">

<p id="input_para"></p>
javascript arrays loops settimeout addeventlistener
1个回答
0
投票

这很好!

let no_of_words =[];
const input_box = document.getElementById('input_box')
const input_para = document.getElementById('input_para')
const input_submit = document.getElementById('input_submit')


function input_result() {
  const info = document.createElement('p')
  info.textContent ='You have exceeded the time'
  info.style.color ='red'
  input_para.appendChild(info)
  //console.log(boxer);
  no_of_words = boxer.length;
  console.log(`You succesfully typed ${no_of_words.lenght} words in 10 seconds`);

}

function lock_input(){
  input_box.style.visibility = 'hidden'
  input_result()

}



const boxer =[]



input_box.addEventListener('keydown', (event)=>{
  lock_input();
  //setTimeout(lock_input, 8000);
  //console.log(`You pressed "${event.key}".`)
  boxer.push(event.key)
}, setTimeout(lock_input, 3000));
© www.soinside.com 2019 - 2024. All rights reserved.