working timer with associated word per minute speed at the end

This commit is contained in:
theo1 2021-02-04 20:31:28 +01:00
parent 567f85f5f3
commit 996cee66a7

View File

@ -1,5 +1,7 @@
<template> <template>
<div class='container'> <div class='container'>
<p id="timer">{{ timer }}<p>
<p id="typing-speed" v-if="!timerRunning">{{ typingSpeed }} words / minute </p>
<div id="prompt-screen"> <div id="prompt-screen">
<span v-for="(letter, index) in letters" :key="index" :class="getSpanClasses(index)">{{ letter }}</span> <span v-for="(letter, index) in letters" :key="index" :class="getSpanClasses(index)">{{ letter }}</span>
</div> </div>
@ -22,13 +24,17 @@ export default {
number: 50, number: 50,
userText: "", userText: "",
activeIndex: 0, activeIndex: 0,
isWrong: false isWrong: false,
timer: 60,
timerRunning: true,
typingSpeed: ""
} }
}, },
async mounted() { async mounted() {
const result = await axios.get("https://random-word-api.herokuapp.com//word?number=" + this.number) const result = await axios.get("https://random-word-api.herokuapp.com//word?number=" + this.number)
this.words = result.data this.words = result.data
this.splitWords() this.splitWords()
this.startTimer()
}, },
methods: { methods: {
isActive(index) { isActive(index) {
@ -66,16 +72,42 @@ export default {
}, },
setNotWrong() { setNotWrong() {
if(this.isWrong) this.isWrong = false if(this.isWrong) this.isWrong = false
},
setTypingSpeed() {
this.typingSpeed = Math.round(this.wordCount)
},
startTimer() {
// Launch countdown
setInterval(() => {
if(this.timerRunning) { this.timer -- }
}, 1000)
},
stopTimer() {
this.timerRunning = false
} }
}, },
computed: { computed: {
cursor() { cursor() {
let c = this.compareStrings(this.userText, this.letters.join("")) let c = this.compareStrings(this.userText, this.letters.join(""))
// Crade
if(c == this.userText.length) { if(c == this.userText.length) {
this.setNotWrong() this.setNotWrong()
} }
return c return c
}, },
wordCount: function() {
// Count number of spaces in correct text up to now.
let correct = this.letters.join("").substring(0, this.cursor)
return ((correct.match(/ /g) || []).length);
}
},
watch: {
timer: function() {
if(this.timer == 0) {
this.stopTimer()
this.setTypingSpeed()
}
},
} }
} }
@ -104,12 +136,17 @@ span.active {
background-color: yellow; background-color: yellow;
} }
span.wrong {
background-color: red;
}
#user-input > input { #user-input > input {
width: 500px; width: 500px;
font-size: 2em; font-size: 2em;
} }
span.wrong { #timer {
background-color: red; font-size: 2em;
font-weight: bold;
} }
</style> </style>