我一直在努力解决这个问题。但是,我已经完成了代码,但我的客户说计算错误。他说(我做了3秒的测试,WPM是298,应该是11,000 wpm,段落的话是290)
这是现场演示网址,请选择一个段落并启动它, http://www.freereadingtest.com/
这是代码
var numWordsInText, myTimer, startTime, wpm;
function SetNumWordsInText(b) {
s = b;
s = s.replace(/(^\s*)|(\s*$)/gi, "");
s = s.replace(/[ ]{2,}/gi, " ");
s = s.replace(/\n /, "\n");
numWordsInText = s.split(" ").length
}
function updateWPM() {
if (wpm = (new Date).getTime() - startTime) wpm /= 1E3, wpm = numWordsInText / wpm, wpm *= 60;
5E4 > wpm && (document.getElementById("wpm1").innerHTML = wpm.toFixed(0));
myTimer = setTimeout(function() {
updateWPM()
}, 500)
}
答案 0 :(得分:0)
所以,我在这里尝试了随机文本。我得到了阅读(const seconds = 3
)并使用setNumWordsInText()
所花费的总秒数。然后,我将这些单词按时间分成几秒钟,以查看每秒有多少单词。然后,我乘以60得到它在一分钟内的单词数。如果这是您正在寻找的,请告诉我。
const setNumWordsInText = b => {
let s = b
s = s.replace(/(^\s*)|(\s*$)/gi, "")
s = s.replace(/[ ]{2,}/gi, " ")
s = s.replace(/\n /, "\n")
return s.split(" ").length
}
const updateWPM = (t, w) => (w / t) * 60
const txt = "Spicy jalapeno bacon ipsum dolor amet rump t-bone andouille, reprehenderit veniam sausage burgdoggen tri-tip pork chop shoulder. In labore pork leberkas bacon, dolor fugiat sunt sint excepteur ball tip landjaeger ut corned beef. T-bone in magna, consectetur do sirloin culpa elit labore meatloaf quis pariatur. Magna veniam capicola consequat, drumstick filet mignon excepteur. Hamburger pork consequat incididunt aliqua tongue cupim nulla burgdoggen leberkas sirloin."
const numWordsInText = setNumWordsInText(txt)
const seconds = 3
const wpm = updateWPM(seconds, numWordsInText)
console.log(wpm)