I have a function that calculates the users words per minute (wpm), it should only be called once after 60 seconds. At the moment I am using if statements and setTimeout, but not getting the desired result.
wpm logic
function wordsdPerMinute() {
if (userInput.toLocaleLowerCase() + ', ' == wordArray[wordCount]) {
set_wordCount(wordCount + 1)
}
if (functionCounter == 0) {
let timeOutID = setTimeout(() => {
set_funcitonCounter(functionCounter + 1)
alert('Times up!, press "Check your highscore!" downbelow')
}, 60000)
clearTimeout(timeOutID)
}
else {
set_funcitonCounter(functionCounter - 1)
wpmCounter()
}
}
function wpmCounter() {
countWPM = (wordCount + 1) / 60;
highscore.push(countWPM)
highscore.sort(function(a,b) {return b-a})
}
How the function is called
<TextInput style={styles.inputText} onChangeText={set_userInput} value={userInput} onKeyPress={(event) => {
if (event.nativeEvent.key == ' ') {
wordsdPerMinute()
}
() => set_reset(initialState)
}}
The result I am getting is the wpmCounter() is executed as many times as it is called after 60 seconds seconds. How can i call the wpmCounter() function only once after 60 seconds?