用户输入的时间为零,即使它在javascript中应该更多
我正在计算用户从开始打字到停止打字的时间。我把两个变量放在起点和终点,然后把它们相互减去。但它总是返回零。如果可能的话,你能给出一个简单的方法来计算wpm吗?最后,我想在用户正确键入10个单词时计算每分钟的单词数。所以我需要从他们开始到完成提示的时间 谢谢 Javascript:用户输入的时间为零,即使它在javascript中应该更多,javascript,html,css,web,wpm,Javascript,Html,Css,Web,Wpm,我正在计算用户从开始打字到停止打字的时间。我把两个变量放在起点和终点,然后把它们相互减去。但它总是返回零。如果可能的话,你能给出一个简单的方法来计算wpm吗?最后,我想在用户正确键入10个单词时计算每分钟的单词数。所以我需要从他们开始到完成提示的时间 谢谢 Javascript: var input = document.getElementById("boch"); input.addEventListener("keyup", function (e
var input = document.getElementById("boch");
input.addEventListener("keyup", function (event) {
if (event.keyCode === 13) {
event.preventDefault();
document.getElementById("bocho").click();
}
});
var element = document.querySelector("#boch");
element.onkeyup = function () {
var value = element.value;
if (value.includes("m")) {
var start = Date.now();
}
if (value.includes("man")) {
document.getElementById('word-1').style.backgroundColor = 'green';
} else {
document.getElementById('word-1').style.backgroundColor = "red";
}
if (value.includes("man become")) {
document.getElementById('word-2').style.backgroundColor = 'green';
} else {
document.getElementById('word-2').style.backgroundColor = "red";
}
if (value.includes("man become as")) {
document.getElementById('word-3').style.backgroundColor = 'green';
} else {
document.getElementById('word-3').style.backgroundColor = "red";
}
if (value.includes("man become as and")) {
document.getElementById('word-4').style.backgroundColor = 'green';
} else {
document.getElementById('word-4').style.backgroundColor = "red";
}
if (value.includes("man become as and through")) {
document.getElementById('word-5').style.backgroundColor = 'green';
} else {
document.getElementById('word-5').style.backgroundColor = "red";
}
if (value.includes("man become as and through find")) {
document.getElementById('word-6').style.backgroundColor = 'green';
} else {
document.getElementById('word-6').style.backgroundColor = "red";
}
if (value.includes("man become as and through find would")) {
document.getElementById('word-7').style.backgroundColor = 'green';
} else {
document.getElementById('word-7').style.backgroundColor = "red";
}
if (value.includes("man become as and through find would here")) {
document.getElementById('word-8').style.backgroundColor = 'green';
} else {
document.getElementById('word-8').style.backgroundColor = "red";
}
if (value.includes("man become as and through find would here and")) {
document.getElementById('word-9').style.backgroundColor = 'green';
} else {
document.getElementById('word-9').style.backgroundColor = "red";
}
if (value.includes("man become as and through find would here and before")) {
document.getElementById('word-10').style.backgroundColor = 'green';
} else {
document.getElementById('word-10').style.backgroundColor = "red";
}
if (value.includes("man become as and through find would here and before")) {
var end = Date.now();
}
let millis = end-start;
console.log(millis)
}
HTML:
答复
人变成
通过这里和之前找到的
进入
```
首先,value.includes(“m”)
每次都是真的,因此每次启动函数时计时器都会重新启动。相反,请使用value==“m”
:
您需要在if
语句外部声明var end
和var start
,并在内部设置它:
var start;
if (value === "m") {
start = Date.now();
}
// other code...
var end;
if (value.includes("man become as and through find would here and before")) {
end = Date.now();
}
let millis = end-start;
另外,我建议在
end
和start
之间留出一个空格end start
可以是一个变量名。你是说=
而不是includes
?它只给出零。你需要对start
执行同样的操作。我将编辑它,它仍然给出NaN,然后是0。@IrfanS而不是值。包括(“m”)
,它应该是值==“m”
。我将再次编辑againSorry,因为它会打扰您,但现在只有在我编辑console.log(millis)时才会给出NaN作为答案
if (value === "m") {
start = Date.now();
}
var start;
if (value === "m") {
start = Date.now();
}
// other code...
var end;
if (value.includes("man become as and through find would here and before")) {
end = Date.now();
}
let millis = end-start;