Hi, I am trying to make a speed typing game where a user has to type the random word on the screen before the timer runs out. I have a time tracker class that keeps track of the time value and a countdown method that returns a promise while the countdown is running using a set interval method in it.
I am having trouble in implementing a way to keep track if the user has typed the word displayed on the screen correctly while the contdown function is running any help is appreciated
/**
* Use to keep track of the timer and display it to the user
*
*/
class timerTracker(){
constructor(time){
this._time = time;
}
/**
* Starts the countdown timer and display the time on the html element
* @element the dom element to display the timer to
* @match callback function to check if the wordinput entered by the user matches the random word
*/
startCountDown(element,match){
const temp = this._time; // temp variable to store initial time
const promise = new Promise((resolve,reject)=>{
setInterval(()=> {
if(this._time <=0){
clearInterval(timer);
this._time = 0;
resolve(this._time);
}
//TODO check function that ensures the word input are equal before resolving
if(match){
this._time = temp;
resolve();
}
this._time--;
let res = this._time > 0 ? this._time/100 : 0;
element.textContent = `${res.toPrecision(this._time.toString().length)}`
},10)
})
}
}