So I have been trying to get this for a thing I am doing but couldn't find any good tutorials on it or questions on it here. All I found was people finding out how to highlight the text with a textarea. In my code the last two params for the function are for the text that displays the current text that is being read.
function speak(text, speed, displaytext, idoftexttodisplay) {
let realspeed = undefined
if (speed) {
realspeed = speed
} else {
realspeed = 1
}
let speech = new SpeechSynthesisUtterance(text)
speech.lang = "en"
speech.rate = realspeed
speechSynthesis.speak(speech)
}